在java中取消引用可能的空指针

时间:2013-10-31 03:51:51

标签: java multithreading graphics nullpointerexception

我刚刚开始进入图形,当我试图获取图形时,我收到错误“运行:     线程“Thread-2”中的异常java.lang.NullPointerException     在gamedev.Display.render(Display.java:97)     在gamedev.Display.run(Display.java:108)     在java.lang.Thread.run(Thread.java:724)“ 我不知道发生了什么事!非常感谢任何帮助。

//The display class for the game
//Crated: 10-30-2013
//Last Modified: 10-30-2013
package gamedev;

import gamedev.Graphics.Render;
import gamedev.Graphics.Screen;
import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import javax.swing.JFrame;

public class Display extends Canvas implements Runnable {

public static final int GAMEWIDTH = 600;
public static final int GAMEHEIGHT = 600;
private static Thread thread;
private static boolean running = false;
private Render render;
private Screen screen;
private BufferedImage img;
private int[] pixels;

public static void main(String[] args) {
    System.out.println("display.main");
    Display gameWindow = new Display();
    JFrame gameFrame = new JFrame();

    gameFrame.add(gameWindow);
    gameFrame.setResizable(false);
    gameFrame.setVisible(true);
    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
    gameFrame.setLocation(dim.width / 2 - gameFrame.getSize().width / 2, dim.height / 2 - gameFrame.getSize().height / 2);
    gameFrame.setSize(GAMEWIDTH, GAMEHEIGHT);
    gameFrame.setTitle("Game Frame");
    gameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    gameWindow.start();

}

public Display() {
    System.out.println("display.Display");
    screen = new Screen(GAMEWIDTH, GAMEHEIGHT);
    img = new BufferedImage(GAMEWIDTH, GAMEHEIGHT, BufferedImage.TYPE_INT_RGB);
    pixels = ((DataBufferInt) img.getRaster().getDataBuffer()).getData();
}

private void start() {
    System.out.println("display.Started");
    if(running)
        return;
    running = true;
    thread = new Thread(this);
    thread.start();
}

private String stop() {
    System.out.println("display.Stopped");
    if (!running) {
        System.out.println("running");
        running = false;
        try {
            thread.join();
        } catch (Exception e) {
            System.exit(0);
            return e.getStackTrace().toString();
        }
        return "Program Stopped";
    } else {
        return "Program Not Stopped";
    }

}

private void tick() {
}

private void render() {
    System.out.println("display.render");
    BufferStrategy bs = this.getBufferStrategy();
    if (bs == null) {
        createBufferStrategy(3);
    }

    for (int i = 0; i < GAMEWIDTH * GAMEHEIGHT; i++) {
        pixels[i] = screen.PIXELS[i];
    }

    screen.Render();

    Graphics g = bs.getDrawGraphics();
    g.drawImage(img, 0, 0, GAMEWIDTH, GAMEHEIGHT, null);
    g.dispose();
    bs.show();
}

@Override
public void run() {
    System.out.println("display.run");
    while (running) {
        tick();
        render();
    }
}

}

1 个答案:

答案 0 :(得分:1)

改变这个:

BufferStrategy bs = this.getBufferStrategy();
if (bs == null) {
    createBufferStrategy(3);
}

为:

BufferStrategy bs = this.getBufferStrategy();
if (bs == null) {
    createBufferStrategy(3);
    bs = this.getBufferStrategy();
}

问题是如果bsnull,则会创建缓冲策略,但不会为bs分配任何内容。