Java Can';找不到NullPointerException错误

Java Can';找不到NullPointerException错误,java,processing,Java,Processing,我正在尝试建立一个蛇游戏,蛇吃方形球体 以前,这个程序运行得很好,但当我几天前运行它时,它对我大喊大叫,因为我发现了一个NullPointerException。我试着寻找引起它的原因,它就在我的Snake课上 以下是主类的代码: Snake s; Score score; //Menu m; int sc1 = 20; PVector food; void setup() { size(700, 700); //m = new menu; //m.show(); s = n

我正在尝试建立一个蛇游戏,蛇吃方形球体

以前,这个程序运行得很好,但当我几天前运行它时,它对我大喊大叫,因为我发现了一个
NullPointerException
。我试着寻找引起它的原因,它就在我的
Snake
课上

以下是主类的代码:

Snake s;
Score score;
//Menu m;
int sc1 = 20;

PVector food;

void setup() {
  size(700, 700);
  //m = new menu;
  //m.show();
  s = new Snake();
  score = new Score();
  //m.startGame();

  frameRate(10);
}

void pickLocation() {
  int cols = width/sc1;
  int rows = height/sc1;
  food = new PVector(floor(random(cols-20)), floor(random(rows-20)));
  food.mult(sc1);
}

void draw() {
  background(51);
  if (s.eat(food)) {
     pickLocation();
     score.addPoints(10);
  }
  pickLocation();

  score.show();
  s.update();
  s.show();
  s.death();
  if (s.dead == true) {
    score.highScores();
  }

  if (score.totalScore != s.i/10) {
    score.totalScore = s.i * 10;
  }

  if (s.dead && score.totalScore < score.highScore) {
    score.totalScore = 0;
  }

  fill(255, 0, 100);
  rect(food.x, food.y, sc1, sc1);
}

void keyPressed() {
  if (keyCode == UP) {
    s.dir(0, -1);
  } else if (keyCode == DOWN) {
    s.dir(0, 1);
  } else if (keyCode == RIGHT) {
    s.dir(1, 0);
  } else if (keyCode == LEFT) {
    s.dir(-1, 0);
  }
}
最后是我的
Snake
类,问题所在:

class Snake {
  float x, y;
  float xSpeed = 1;
  float ySpeed = 0;
  int total = 0;
  ArrayList<PVector> tail = new ArrayList<PVector>();
  boolean dead = false;
  int i = 0;

  Snake() {
  }

  boolean eat (PVector pos) {
    float d = dist(x, y, pos.x, pos.y);
    if (d < 1) {
      total++;
      return true;
    } else {
      return false;
    }
  }

  void dir(float x, float y) {
    xSpeed = x;
    ySpeed = y;
  }

  void death() {
    for (i = 0; i < tail.size(); i++) {
      PVector pos = tail.get(i);
      float d = dist(x, y, pos.x, pos.y);
      if (d < 1) {
        println("starting over");
        total = 0;
        tail.clear();
        dead = true;
      } else {
        dead = false;
      }
    }
  }

  void update() {
    if (total > 0) {
      if (total == tail.size() && !tail.isEmpty()) {
        tail.remove(0);
      }
      tail.add(new PVector(x, y));
    }

    x = x + xSpeed * sc1;
    y = y + ySpeed * sc1;

    x = constrain(x, 0, width-sc1);
    y = constrain(y, 0, height-sc1);
  }

  void show() {
    fill(0, 255, 0);
    for (PVector v : tail) {
      rect(v.x, v.y, sc1, sc1);
    }
    rect(x, y, sc1, sc1);
    //rect(x, y, w, h);
  }
}
类蛇{
浮动x,y;
float xSpeed=1;
浮动Y速度=0;
int-total=0;
ArrayList tail=新的ArrayList();
布尔死=假;
int i=0;
蛇(){
}
布尔eat(PVector位置){
浮动d=距离(x,y,位置x,位置y);
if(d<1){
总计++;
返回true;
}否则{
返回false;
}
}
void dir(浮动x,浮动y){
xSpeed=x;
y速度=y;
}
无效死亡(){
对于(i=0;i0){
if(total==tail.size()&&!tail.isEmpty()){
尾部。移除(0);
}
添加(新的PVector(x,y));
}
x=x+x速度*sc1;
y=y+y速度*sc1;
x=约束(x,0,宽度-sc1);
y=约束(y,0,高度-sc1);
}
无效显示(){
填充(0,255,0);
用于(PVector v:尾部){
rect(v.x,v.y,sc1,sc1);
}
rect(x,y,sc1,sc1);
//rect(x,y,w,h);
}
}

我的问题是,是否有人能识别错误,我应该怎么做才能纠正错误。你需要养成正确理解错误的习惯。你知道这句话是在抛出NPE:

float d = dist(x, y, pos.x, pos.y);
接下来,您需要了解该行上每个变量的值。你可以把它们打印出来:

boolean eat (PVector pos) {
  println("x: " + x);
  println("y: " + y);
  println("pos: " + pos);
  float d = dist(x, y, pos.x, pos.y);
如果执行此操作,您将看到以下输出:

x: 0.0
y: 0.0
pos: null
这告诉您您的
pos
变量是
null
,这就是导致
NullPointerException
的原因

现在,您可以追溯代码,以了解为什么给
eat()
函数一个
null
参数


今后,请将您的问题缩小到a,而不是发布整个程序。

哪一行抛出了错误?你能链接堆栈跟踪吗?抛出错误的是第14行。snake类中的第14行?这就是它一直带我去的地方。这一行<代码>浮动d=距离(x,y,位置x,位置y)只是确保它对我和对你都一样。在@Kevin Workman的基础上,我做了一些调试来回答有人问我的评论,除此之外,我做了一些调试,就像你要求我做的那样,我相信这与函数“eat”的执行方式有关。现在我可能错了,但我认为问题就在这里。@JordanGreen对,您想看看如何调用
eat()
函数。你在传递什么价值?为什么变量
为null
?您何时设置该变量?在调用
eat()
之前,是否确定已设置该值?提示:不是。当我调用
eat
函数时,传递的参数是
food
,这是一个
PVector
,传递该参数是因为它是执行其工作所需的参数。由于
food.x
food.y
均为零,因此假定参数为null,这就是为什么我得到的
NullPointerException
@JordanGreen不太正确。
0,0
PVector
不是
null
。它是
null
,因为您在使用它时没有将它设置为值。什么时候设置它的值?这是在你尝试使用它之前还是之后发生的?逐行跟踪代码以准确了解它在做什么。我不知道在
SnakeGame
类中我在哪里给出了变量。我肯定知道它不会出现在
菜单
评分
课程中。它可能在
Snake
类中吗?
x: 0.0
y: 0.0
pos: null