无法找到NullPointerException错误

时间:2018-06-07 16:21:01

标签: 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 = 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);
  }
}

我现在评论的菜单。

Score类:

class Score {
  int totalScore = 0; //will add the total score to the 
  int highScore; //will hold the user's high score in it.
  int tempScore; //will hold the user's score after the snake dies.

  Score() {
  }

  //this method is used when the snake eats the
  //food. Eating the food will give 10 points to it.
  void addPoints(int x) {
    totalScore = totalScore + x;
  }

  //this method will calculate to see if the user
  //has a new high score, only if the snake has
  //officially died.
  void highScores() {
    if (totalScore > highScore) {
      text("new highscore!", height/2, width/2);
      highScore = totalScore;
      totalScore = 0;
    }
  }

  void show() {
    text("Score: " + totalScore, 20, 20);
    text("High Score: " + highScore, 20, 40);
  }
}

最后,我的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);
  }
}

我的问题是,是否有什么东西可以识别错误,我应该怎么做才能解决这样的错误。

1 个答案:

答案 0 :(得分:1)

你需要养成debugging your code的习惯才能准确理解正在发生的事情。你知道这条线正在抛弃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参数。

将来,请将您的问题缩小到MCVE,而不是发布整个计划。