我目前正在尝试将现有的Processing项目集成到Eclipse中。有两个类,TagCloud和Tag,前者是后者的控制器。在TagCloud中,我有以下代码片段:
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();
}
tags是Tag对象的ArrayList,tagsMap是HashMap(String,Tag)类型的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);
}
}
}
parent是在Tag的构造函数中传递的PApplet。
答案 0 :(得分:0)
所以我发现了错误。
在我的TagsCloud
对象的构造函数中,我正在初始化Tag
对象并尝试将一个ref传递给TagsCloud对象。但是,TagsCloud
构造函数尚未完成执行,因此TagsCloud
对象尚未完全初始化,因此NullPointerException