0
我做了一個使用二維數組繪製網格的程序,但是我得到一個空指針異常。這裏是我的代碼:處理圖形錯誤
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)
我究竟做錯了什麼?
不相關的問題:A碼遵循的慣例,大多數Java開發人員是用小寫字母開頭的變量名。 – MrSmith42