Can't Find NullPointerException

I am trying to code a Snake Game in which the snake eats square orbs

When I was running the program a couple of days ago, the program throws a NullPointerException, which is saying that Line 14 in my Snake class is causing the exception, is there anyone who sees the error and can help fix it?

Here is my code:

Here is the code from my main class (SnakeGame.pde):

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

PVector food;

void setup() {
  size(700, 700);
  output = createWriter("logfile.txt");
  //m = new Menu();
  //m.show();
  s = new Snake();
  score = new Score();
  //m.startGame();
  output.println(s.eat(food));
  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);
  }
}

Here is the code from my Menu class:

class Menu {
  int x1, y1;
  int x2, y2;
  int w1, h1;
  int h2, w2;
  boolean keyUsed = false;
  PImage img;
  PShader opaque;
  Menu() {
    x1 = (width/2)-50;
    y2 = (height/2)-50;
    w1 = 60;
    h1 = 60;
    img = loadImage("Greener Game Development.png");
  }

  void show() {
    showLogo();
    textSize(32);
    text("The Snake Game", width/2, 50);
    textSize(24);
    text("Play game", w1+10, h1+10);
  }

  void startGame() {
    if (key == ENTER) {
      keyUsed = true;
    }
    while (keyUsed == true) {
      closeMenu();
    }
  }
  
  //this function removes the entire menu
  //used in the startGame function.
  void closeMenu() {
    if (key == ENTER) {
      x1 = 0;
      x2 = 0;
      y1 = 0;
      y2 = 0;
      h1 = 0;
      h2 = 0;
    }
  }
  
  //this function shows the developer's logo
  //in the menu.
  //used in the show function.
  void showLogo() {
    int showTime = 0;
    showTime++;
    image(img, width/2-40, height/2-40);
    if (showTime == showTime + 1 && showTime < 5) {
      filter(opaque);
    }
  }
}

Here is the code from my Score class:

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);
  }
}

Here is the code from my Snake Class:

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 Like

If you post your question to multiple sites, please link between your questions so we know what help you’ve already received.

This question has already been answered here:

Okay, do you have any idea on what to do then, because the last person on StackOverflow gave me advice for C++ and another demanded a log file.

I’m talking about the answer I posted here:

Side note, but you’ll get a bunch of confused people if you tag your Processing question with the Java tag. See also:

1 Like