java.lang.NullPointerException的线程错误

时间:2015-08-10 14:18:39

标签: java multithreading swing nullpointerexception

所以我做了我的第一个Java Game Called Chase,但有时我得到这个错误

 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)

这是我的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();
    }

}

0 个答案:

没有答案