我制作了一个使用二维数组绘制网格的程序,但出现空指针异常。这是我的代码:
Cell[][] Cells;
void setup(){
size(500,500);
background(255);
for (int x = 0; x < 50; x++) {
for (int y = 0; y < 50; y++) {
Cells[x][y] = new Cell(round(random(255)),x * 10,y * 10,10,10);
}
}
}
void draw(){
for (int x = 0; x < 50; x++) {
for (int y = 0; y < 50; y++) {
Cells[x][y].Draw();
}
}
}
class Cell{
int X;
int Y;
int Width;
int Height;
int Color;
Cell(int C,int X,int Y,int Width,int Height){
Color = C;
X = X;
Y = Y;
Width = Width;
Height = Height;
}
void Draw(){
fill(Color);
rect(X,Y,Width,Height);
}
}
这是错误消息:
Exception in thread "Animation Thread" java.lang.NullPointerException
at Grid.setup(Grid.java:27)
at processing.core.PApplet.handleDraw(PApplet.java:2103)
at processing.core.PGraphicsJava2D.requestDraw(PGraphicsJava2D.java:190)
at processing.core.PApplet.run(PApplet.java:2006)
at java.lang.Thread.run(Thread.java:662)
我究竟做错了什么?