我正在将数组更改为arrayList,有几个试验有错误"NullPointerException",代码简化如下所示,当mousePressed创建一个矩形.但仍然有同样的错误.问题是什么?
ArrayList textlines;
int xpos=20;
int ypos=20;
void setup() {
size(1200, 768);
ArrayList textlines = new ArrayList();
//(Line)textlines.get(0) =textlines.add(new Line(xpos, ypos));
}
void draw() {
}
void mousePressed() {
textlines.add(new Line(xpos, ypos));
for (int i=0; i<textlines.size(); i++) {
Line p=(Line)textlines.get(i);
p.display();
}
}
class Line {
int x;
int y;
Line(int xpo, int ypo) {
x =xpo;
y =ypo;
}
void display() {
fill(50, 50, 50);
rect(x, y, 5, 5);
}
}
Run Code Online (Sandbox Code Playgroud)