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;

public synchronized void stop(){
        running = false;
    }catch(Exception e){

public void run() {
    long lastTimeChecked = System.nanoTime();
    int frames = 0;
        try {
        } catch (InterruptedException e){
        if(System.nanoTime() - lastTimeChecked >= 1000000000){
            FPS = frames;
            frames = 0;
            lastTimeChecked = System.nanoTime();

private void tick() {

private void render(){
    BufferStrategy bs = this.getBufferStrategy();
    if(bs == null){
    Graphics g = bs.getDrawGraphics();
    g.fillRect(0, 0, WIDTH, HEIGHT);

    g.setFont(new Font("Dialog", Font.BOLD, 18));
    g.drawString("" + FPS, 5, 20);

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



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.fillRect(x, y, 16, 16);


Thanks in advance!


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.

|improve this answer|||||
  • \$\begingroup\$ Okay I moved player = new player above new Window because the run method is called in the Window class. Thank you for solving this for me! It has been very annoying to me. \$\endgroup\$ – TJRelytS Mar 22 '17 at 21:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.