2013-10-01 30 views
-2

我正在尝试使用以下代码更改顶点v4的样式。尽管改变了风格,但我仍然遇到了错误。我试图做其他行为,而不是像setVisible(false),它的工作原理。所以它一定是我正在使用的方法。我应该改变什么?更改顶点样式的错误

  public class graphgen extends JFrame { 

       JFrame frame ; 
       static JGraph jgraph ; 

       final static mxGraph graph = new mxGraph() { 


       final static mxGraphComponent graphComponent = new mxGraphComponent(graph); 

       Object cell ; 
       Object v1, v2, v3, v4, v5, v6, v7, v8, w1, w2, w3, w4, w5, w6, w7, w8, w9, w10, w11, w12, w13, w14, w15, w16, w17, w18, z1, z2, z3, z4, y1, y2, y3, y4, y5, y6 ; 

       private static final int OPACITY_PALE = 20; 
       private static final int OPACITY_HL = 100; 


       public graphgen() { 



        gen(); 

       } 

       public void gen(){ 

       Hashtable<String, Object> oldstyle = new Hashtable<String, Object>(); 
       oldstyle.put(mxConstants.STYLE_OPACITY, OPACITY_PALE); 
       .... 

       Hashtable<String, Object> newstyle = new Hashtable<String, Object>(); 
       newstyle.put(mxConstants.STYLE_OPACITY, OPACITY_HL); 
       ... 


stylesheet.putCellStyle(oldstyle, oldstyle); 
    stylesheet.putCellStyle(newstyle, newstyle); 

Object parent = graph.getDefaultParent(); 

       graph.getModel().beginUpdate(); 
        try 
        {.... 

    Object v4 = graph.insertVertex(parent, null, "label", 380, 80, 80, 
        30, oldstyle); 
       ... 

       if (GC.contains("aaa")) { 

          graph.getView().getState(v4).setStyle(newstyle); 

          graphComponent.refresh(); 
          graph.repaint(); 
        } 

       ....} 
        finally 
        { 
         graph.getModel().endUpdate(); 
        } 



      getContentPane().add(graphComponent); 
      add(graphComponent); 

      ... 
     } 

      public static void main(String[] args) 
       { 
         graphgen frame = new graphgen(); 

        frame.pack(); 
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        frame.setResizable(false); 
        frame.setSize(1600, 1200); 
        frame.setVisible(true); 


       } 
      } 

我得到以下错误:

Exception in thread "main" java.lang.NullPointerException 
    at graphgen.gen(graphgen.java:482) // line with raph.getView().getState(v4).changeStyle(newstyle); 
+0

如果您没有将多个方法链接在一起,那么确切地说'null'会更容易。 – kiheru

+0

已更新的代码。 @kiheru – user2598911

+0

在该行有三个可以为'null'的东西:'graph','getView()'的返回值和'getState(v4)'的返回值。第一种是不太可能的,除非在getModel()调用之后重新分配'graph'。使用临时变量来存储这些值有助于检查哪些变量导致问题。 – kiheru

回答

1

用途:

mxGraph.setCellStyle(String style, Object[] cells) 

代替。

+0

已经尝试过。不工作。无论如何,我找到了一个不同的解决方案,不是最有效的,但仍然是我的工作。随着插入每个顶点的继续ifs。 – user2598911