2014-11-22 40 views
0

我目前正在嘗試將現有的Processing項目集成到Eclipse中。有兩個類,TagCloud和Tag,前者是後者的控制者。在TagCloud,我有以下代碼片段:將Processing集成到Eclipse中時出現NullPointerException

for (int i = 0; i < NUM_RELATIONS; i++) { 
      println("i: " + i); 
      tags.get(i).refTagsMap(tagsMap); 
      tags.get(i).setLinks(); 
      //Randomly assign a location to the first tag. 
      System.out.println("Tag from map: " + tags.get(i)); 
      for(int j = 0; j < tags.size(); j++) 
       System.out.println(j + " " + tags.get(j)); 
      tags.get(i).drawTag(); 
     } 

     for(int i = 0; i < NUM_RELATIONS; i++) { 
      println("Drawing tags for tag: " + i); 
      tags.get(i).drawLinks(); 
     } 

標籤是標籤對象的ArrayList,tagsMap是該類型的HashMap(字符串,標記)的一個HashMap中。 但是,代碼在tags.get(i).drawTag()方法中斷並顯示以下異常。

Exception in thread "main" java.lang.RuntimeException: java.lang.NullPointerException 
    at processing.core.PApplet.runSketch(PApplet.java:10713) 
    at processing.core.PApplet.main(PApplet.java:10525) 
    at TagCloud.main(TagCloud.java:97) 
Caused by: java.lang.NullPointerException 
    at processing.core.PApplet.noFill(PApplet.java:14574) 
    at Tag.drawTag(Tag.java:65) 
    at TagCloud.<init>(TagCloud.java:87) 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) 
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) 
    at java.lang.reflect.Constructor.newInstance(Constructor.java:408) 
    at java.lang.Class.newInstance(Class.java:438) 
    at processing.core.PApplet.runSketch(PApplet.java:10711) 
    ... 2 more 

下面是drawTag()drawLink方法

public void drawTag() { 
    parent.noFill(); 
    parent.stroke(255); 
    parent.strokeWeight(3);  
    float x = location.x; 
    float y = location.y; 
    //Create the rectangle frame 
    frame = parent.createShape(parent.RECT, x, y, 100, 80); 
    //Add it to the canvas 
    parent.shape(frame); 

    //Write the text 
    parent.text(topic, x + 15, y + 25); 
} 


public void drawLinks() { 
    System.out.println(); 
    parent.stroke(255); 
    parent.strokeWeight(1); 
    for(int i = 0; i < linkKeys.size(); i++) 
    { 
     Tag linkedTag = links.get(linkKeys.get(i)); 
     System.out.println(linkedTag); 
     int x = (int)linkedTag.getLocation().x; 
     int y = (int)linkedTag.getLocation().y; 
     PShape line = parent.createShape(parent.LINE, location.x, location.y, x, y); 
     //Add the shape to the 
     parent.shape(line); 
     linkLines.add(line); 
    } 
    } 
} 

父母是在標籤的構造函數傳遞一個PApplet。

+0

是在TagCloud.main(TagCloud.java:97) – MeBigFatGuy 2014-11-22 16:32:38

+0

哪一行上述樣品中這是主函數調用標籤雲: PApplet.main(new String [] {「--present」,「TagCloud」}); – Satre 2014-11-22 16:33:32

+0

我在'drawTag()'的開頭添加了一個'System.out.println(parent)'行,它打印出下列內容:'TagCloud [panel0,0,0,0x0,invalid,layout = java.awt.FlowLayout ]',這意味着它不是null,對吧? – Satre 2014-11-22 16:39:36

回答

0

所以我發現了錯誤。 在我的TagsCloud對象的構造函數中,我正在初始化Tag對象,並試圖將一個ref傳遞給它的TagsCloud對象。然而,TagsCloud構造尚未完成執行並因此TagsCloud對象尚未完全初始化,因此NullPointerException

相關問題