-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);
如果您沒有將多個方法鏈接在一起,那麼確切地說'null'會更容易。 – kiheru
已更新的代碼。 @kiheru – user2598911
在該行有三個可以爲'null'的東西:'graph','getView()'的返回值和'getState(v4)'的返回值。第一種是不太可能的,除非在getModel()調用之後重新分配'graph'。使用臨時變量來存儲這些值有助於檢查哪些變量導致問題。 – kiheru