Thursday, August 29, 2019

java - Tick() method error when I run it. HELP!


Okay so I get this error:


Exception in thread "Thread-2" java.lang.NullPointerException

at basic.game.here.Game.tick(Game.java:71)
at basic.game.here.Game.run(Game.java:54)
at java.lang.Thread.run(Unknown Source)

Here is the code it says the error relates to:


package basic.game.here;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Font;

import java.awt.Graphics;
import java.awt.image.BufferStrategy;

import basic.game.here.tiles.Ground;

public class Game extends Canvas implements Runnable {

private static final long serialVersionUID = 1L;

public static final int WIDTH = 960, HEIGHT = WIDTH / 12 * 9;


private Thread thread;

private boolean running = false;

private int FPS = 0;

private World world;
private Player player;


public Game(){
new Window(WIDTH, HEIGHT, "BASIC", this);

world = new World();

player = new Player(500, 200);

}

public synchronized void start(){

thread = new Thread(this);
running = true;
thread.start();
}

public synchronized void stop(){
try{
thread.join();
running = false;
}catch(Exception e){

e.printStackTrace();
}
}

public void run() {
long lastTimeChecked = System.nanoTime();
int frames = 0;
while(running){
tick();
render();

try {
Thread.sleep(6);
} catch (InterruptedException e){
e.printStackTrace();
}
frames++;
if(System.nanoTime() - lastTimeChecked >= 1000000000){
FPS = frames;
frames = 0;
lastTimeChecked = System.nanoTime();

}
}
}

private void tick() {
player.tick();
}

private void render(){
BufferStrategy bs = this.getBufferStrategy();

if(bs == null){
this.createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
g.setColor(Color.BLUE);
g.fillRect(0, 0, WIDTH, HEIGHT);

g.setColor(Color.YELLOW);
g.setFont(new Font("Dialog", Font.BOLD, 18));

g.drawString("" + FPS, 5, 20);

//g.drawString("(" + player.x + ", " + player.y + ")", 5, 40);

world.render(g);
player.render(g);

g.dispose();
bs.show();
}


public static void main(String[] args){
new Game();
}

}

Here is the player class because I think it is related to this:


package basic.game.here;


import java.awt.Color;
import java.awt.Graphics;

public class Player {

private int x, y;

public Player(int x, int y){
this.x = x;
this.y = y;

}

public void tick(){

}

public void render(Graphics g){
g.setColor(Color.CYAN);
g.fillRect(x, y, 16, 16);
}


}

Thanks in advance!



Answer



Your crash message indicates that Game.tick() is the source of the null reference exception:


>            Exception in thread "Thread-2" java.lang.NullPointerException at
> -- see --> basic.game.here.Game.tick(Game.java:71)

Game.tick() itself contains nothing but a call to player.tick(), so player is probably null. The player tick function is empty, so even if there were some kind of callstack trimming due to inlining, the only thing that can be null in this code is player.



Put a breakpoint on the player.tick() call in Game.tick() and verify that player is non-null; you do initialize it in the Game constructor, but after you pass the game to the window. Perhaps the window class is calling run, and thus tick() before you have executed the line of code that initializes the player.


You could also try moving the player = new Player... line up above the new Window... line.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...