gpt4 book ai didi

java - 处理图形错误

转载 作者:行者123 更新时间:2023-12-01 14:53:59 25 4
gpt4 key购买 nike

我编写了一个使用二维数组绘制网格的程序,但出现空指针异常。这是我的代码:

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)

我做错了什么?

最佳答案

您错过了创建数组:

Cell[][] cells = new Cell[50][50];

关于java - 处理图形错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14484277/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com