0

So I have made my first Java Game Called Chase, but sometimes I get this error

 Exception in thread "Thread-2" java.lang.NullPointerException
at java.util.LinkedList.node(LinkedList.java:577)
at java.util.LinkedList.get(LinkedList.java:477)
at net.bitify.main.Handler.tick(Handler.java:13)
at net.bitify.main.Game.tick(Game.java:143)
at net.bitify.main.Game.run(Game.java:112)
at java.lang.Thread.run(Thread.java:745)

This is my source code for game.java

    package net.bitify.main;

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

import java.awt.Graphics;

import java.awt.image.BufferStrategy;

import java.util.Random;

import net.bitify.main.entity.ID;

import net.bitify.main.entity.Player;

import net.bitify.main.map.HUD;

import net.bitify.main.map.Spawn;

public class Game extends Canvas implements Runnable {


private static final long serialVersionUID = -726038495289796084L;

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

    public Player player;

    public static int Trail = 1;

    private Thread thread;
    private boolean running = false;
    private Random r;

    private Handler handler;
    private HUD hud;
    private Spawn spawner;
    private Menu menu;
    private Help help;
    private Shop shop;
    private Gameover gameover;

    public enum STATE {
        Menu, PreMenu, Game, Pause, Help, Shop, Gameover;
    }

    public static STATE gameState = STATE.Menu;

    public Game() {
        handler = new Handler();
        menu = new Menu(handler);
        help = new Help(handler);
        shop = new Shop();
        gameover = new Gameover();
        this.addKeyListener(new KeyInput(handler));
        this.addMouseListener(menu);
        this.addMouseListener(help);

        new Window(WIDTH, HEIGHT, "Chase Survival 1.0", this);

        hud = new HUD(handler);
        spawner = new Spawn(handler, hud);

        r = new Random();

        if (gameState == STATE.Game) {
            handler.addObject(new Player(Game.WIDTH / 2 - 16, Game.HEIGHT / 2, ID.Player, handler));
        } else {
            for (int i = 0; i < 25; i++) {

                handler.addObject(
                        new MenuParticle(r.nextInt(Game.WIDTH), r.nextInt(Game.HEIGHT), ID.MenuParticle, handler));
            }
            Gameover.Money = 0;

        }

    }

    public synchronized void start() {
        thread = new Thread(this);
        thread.start();
        running = true;
    }

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

    @Override
    public void run() {
        // Game Loop
        this.requestFocus();
        long lastTime = System.nanoTime();
        double amountOfTicks = 60.0;
        double ns = 1000000000 / amountOfTicks;
        double delta = 0;
        int frames = 0;
        long timer = System.currentTimeMillis();
        while (running) {
            long now = System.nanoTime();
            delta += (now - lastTime) / ns;
            lastTime = now;
            while (delta >= 1) {
                tick();
                delta--;
            }
            if (running)
                render();
            frames++;

            if (System.currentTimeMillis() - timer > 1000) {
                timer += 1000;
                System.out.println(" FPS: " + frames);
                frames = 0;
            }

        }
        stop();
    }

    private void tick() {

        if (gameState == STATE.Game) {

            handler.tick();
            spawner.tick();
            hud.tick();

        } else if (gameState == STATE.Pause) {
            hud.tick();
        } else if (gameState == STATE.Menu) {
            menu.tick();
            handler.tick();
        } else if (gameState == STATE.Help) {
            help.tick();
            handler.tick();
        } else if (gameState == STATE.Shop) {
            shop.tick();
            handler.tick();
        } else if (gameState == STATE.PreMenu) {
            hud.tick();
        } else if (gameState == STATE.Gameover) {
            gameover.tick();
            handler.tick();
        }
    }

    private void render() {
        BufferStrategy bs = this.getBufferStrategy();
        if (bs == null) {
            this.createBufferStrategy(3);
            return;
        }

        Graphics g = bs.getDrawGraphics();

        g.setColor(Color.green);
        g.fillRect(0, 0, WIDTH, HEIGHT);

        handler.render(g);

        if (gameState == STATE.Game) {
            hud.render(g);
        } else if (gameState == STATE.Pause) {
            hud.render(g);

            g.setColor(Color.white);
            g.drawString("Paused", WIDTH / 2 - 24, HEIGHT / 2);
            g.drawString("Press P to unpause", WIDTH / 2 - 60, HEIGHT / 2 + 16);
        } else if (gameState == STATE.Menu) {
            menu.render(g);
        } else if (gameState == STATE.Help) {
            help.render(g);
        } else if (gameState == STATE.Shop) {
            shop.render(g);
        } else if (gameState == STATE.Gameover) {
            gameover.render(g);
        } else if (gameState == STATE.PreMenu) {

            hud.render(g);

            g.setColor(Color.darkGray);
            g.fillRoundRect(180, 220, 350, 150, 90, 90);
            g.setColor(Color.lightGray);
            g.fillRoundRect(185, 225, 340, 140, 90, 90);
            g.setColor(Color.black);
            g.drawRoundRect(185, 225, 340, 140, 90, 90);

            g.setColor(Color.black);
            g.drawString("Are you sure you want to proceed to the menu?", WIDTH / 2 - 134, HEIGHT / 2);
            g.drawString("This will end your current game.", WIDTH / 2 - 92, HEIGHT / 2 + 18);
            g.drawString("Press 'Backspace' to go back to the game.", WIDTH / 2 - 120, HEIGHT / 2 + 36);
            g.drawString("Press 'Enter' to go to the menu.", WIDTH / 2 - 90, HEIGHT / 2 + 54);
        }
        g.dispose();
        bs.show();

    }

    public static float clamp(float var, float min, float max) {
        if (var >= max) {
            return var = max;
        } else if (var <= min) {
            return var = min;
        } else {
            return var;
        }
    }

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

}
Kara
  • 6,115
  • 16
  • 50
  • 57
  • 1
    You're not showing the offending code: `at net.bitify.main.Handler.tick(Handler.java:13)`. Line 13 of the Handler.java class. If you've done even a little searching on solving a NullPointerException (NPE), you'll know that the most important bit of information that we need is the exception's associated stacktrace and some identification of the line that causes it, something that the stacktrace will tell you. – Hovercraft Full Of Eels Aug 10 '15 at 14:19
  • 1
    More importantly, you need to learn the general concepts of how to debug a NPE (NullPointerException). **You should critically read your exception's stacktrace to find the line of code at fault, the line that throws the exception, and then inspect that line carefully**, find out which variable is null, and then trace back into your code to see why. You will run into these again and again, trust me. – Hovercraft Full Of Eels Aug 10 '15 at 14:20
  • So would i have to add the code and which code? Sorry I am new to this stuff – Anmol Brar Aug 10 '15 at 14:20
  • I told you -- the Handler class. – Hovercraft Full Of Eels Aug 10 '15 at 14:21

0 Answers0