Java Graphics NullPointerException当完全相同的东西在其他地方工作时

时间:2016-10-21 00:51:01

标签: java graphics

我目前正在使用java Graphics制作一个简单的游戏并使用多个菜单。比如Help,Main,Game本身,Lose和Win。所有菜单都有自己的渲染功能,根据游戏状态调用。除“赢”菜单外,所有菜单均有效。发布了'Lose'和'Win'菜单。它们几乎是一样的,但“赢”不起作用。

在主游戏中渲染:

private void render() {
    BufferStrategy bs = this.getBufferStrategy();
    if(bs == null) {
        this.createBufferStrategy(3);
        return;
    }
    Graphics g = bs.getDrawGraphics();
    g.setColor(Color.black);
    g.fillRect(0, 0, WIDTH, HEIGHT);

    if(GameState == STATE.Game) {
        handler.render(g);
        hud.render(g);
    } else if(GameState == STATE.Menu) {
        menu.render(g);
    } else if(GameState == STATE.Help) {
        help.render(g);
    } else if(GameState == STATE.EndMenu) {
        endMenu.render(g);
    } else if(GameState == STATE.Win) {
        win.render(g);
    }

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

赢:

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

public class Win {

public Win(){}

public void render(Graphics g) {
    g.setColor(Color.WHITE);
    g.setFont(new Font("SansSerriff", Font.BOLD, 100));
    g.drawString("YOU WIN!!!", Game.WIDTH/2 - 280, Game.HEIGHT/2 - 50);

    g.setFont(new Font("SansSerriff", Font.PLAIN, 60));
    g.drawString("Player Again", 360, 600);
    g.drawRect(250, 535, 500, 100);
}
}

失去:

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

public class EndMenu {

public EndMenu() {}

public void render(Graphics g) {
    g.setColor(Color.WHITE);
    g.setFont(new Font("SansSerriff", Font.BOLD, 100));
    g.drawString("YOU LOSE!!", Game.WIDTH/2 - 280, Game.HEIGHT/2 - 50);

    g.setFont(new Font("SansSerriff", Font.PLAIN, 60));
    g.drawString("Play Again", 360, 600);
    g.drawRect(250, 535, 500, 100);
}

}

错误讯息:

Exception in thread "Thread-2" java.lang.NullPointerException
at Main.Game.render(Game.java:132)
at Main.Game.run(Game.java:86)
at java.lang.Thread.run(Unknown Source)

非常感谢所有帮助。谢谢!

修改

游戏:

    package Main;

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

public class Game extends Canvas implements Runnable {
    private static final long serialVersionUID = 2287623113957789623L;

    public static final int WIDTH = 1000, HEIGHT = WIDTH/12 * 9;
    private Thread thread;
    public static boolean running = false;
    private  Handler handler;
    private HUD hud;
    private Spawner spawner;
    private Menu menu;
    private Help help;
    private EndMenu endMenu;
    private Win win;
    public static STATE GameState = STATE.Win;

    //----------Game()----------//
    //---------------------------------------------------------------------------
    public Game() {
        handler = new Handler();
        menu = new Menu(this, handler);
        help = new Help();
        endMenu = new EndMenu();
        this.addKeyListener(new KeyInput(handler));
        this.addMouseListener(menu);

        new Window(WIDTH, HEIGHT, "Wave", this);
        hud = new HUD();
        spawner = new Spawner(handler, hud);
    }

    //----------Game Modes----------//
    //---------------------------------------------------------------------------
    public enum STATE {
        Menu,
        Game,
        Help,
        EndMenu,
        Win,
    }

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

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

    //----------run()-----------//
    //---------------------------------------------------------------------------
    public void run() {
        this.requestFocus();
        long lastTime = System.nanoTime();
        double amountOfTicks = 60.0;
        double ns = 1000000000 / amountOfTicks;
        double delta = 0;
        long timer = System.currentTimeMillis();
        int frames = 0;
        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();
    }

    //----------tick()----------//
    //---------------------------------------------------------------------------
    private void tick() {
        if(GameState == STATE.Game) {
            handler.tick();
            hud.tick();
            spawner.tick();
        }
        if(HUD.HEALTH <= 0 && HUD.HEALTH2 <= 0) {
            GameState = STATE.EndMenu;
        }
    }

    //----------render()----------//
    //---------------------------------------------------------------------------
    private void render() {
        BufferStrategy bs = this.getBufferStrategy();
        if(bs == null) {
            this.createBufferStrategy(3);
            return;
        }
        Graphics g = bs.getDrawGraphics();
        g.setColor(Color.black);
        g.fillRect(0, 0, WIDTH, HEIGHT);

        if(GameState == STATE.Game) {
            handler.render(g);
            hud.render(g);
        } else if(GameState == STATE.Menu) {
            menu.render(g);
        } else if(GameState == STATE.Help) {
            help.render(g);
        } else if(GameState == STATE.EndMenu) {
            endMenu.render(g);
        } else if(GameState == STATE.Win) {
            win.render(g);
        }

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

    //----------clamp()----------//
    //--------------------------------------------------------------------------
    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;
    }

    //----------main()----------//
    //--------------------------------------------------------------------------
    public static void main(String args[]) {
        new Game();
    }
}
Game.java.132: win.render(g);
Game.java.86: render();

1 个答案:

答案 0 :(得分:0)

你在这条线上获得NPE:

win.render(g);

因为win为空。您已对其进行了定义,但尚未为其分配值。

根据您的代码,您应该更新Game构造函数以包含以下行:

win = new Win();