所以我有一个处理按钮的类,我有一个矩形数组,其中包含 2 个单独的矩形形状。现在,当我创建一个变量来检索数组的第 0 个索引时,它会给我一个 nullpointerexception 我一直在挠头,我已经明确声明并初始化了数组,并使其大小适合包含 2 个矩形,并且已将这些分配给索引。我一定是错过了一些我似乎无法弄清楚的非常小的东西。
下面我放了相关的代码:
public class MenuButton {
private int height;
private int width;
private float positionX;
private float positionY;
//private ArrayList<Rectangle> rects;
private Rectangle rects[];
private Rectangle play;
private Rectangle touchToPlay;
private boolean isTouched;
public MenuButton(int height, int width, float positionX, float positionY){
this.height = height;
this.width = width;
this.positionX = positionX;
this.positionY = positionY;
isTouched = false;
Rectangle rects[] = new Rectangle[2];
play = new Rectangle(positionX, positionY, width, height);
touchToPlay = new Rectangle(positionX, positionY, width, height);
//can clean this up by introducing initButtons() to assign buttons to
//indexes of the array
rects[0] = play;
rects[1] = touchToPlay;
}
public boolean isClicked(int index,float screenX, float screenY){
//ERROR IS BELOW THIS LINE
Rectangle rect = rects[0];
return rect.contains(screenX, screenY);
}
最佳答案
关于java - 从矩形数组中检索矩形时出现空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26596408/