我在java中创建了一个非常基本的2D游戏,我在运行Windows 7的桌面上编写了它。然后我将我的项目导出到我的Macbook并得到以下错误:
Exception in thread "Thread-0" java.lang.NullPointerException
at java.awt.Component$FlipBufferStrategy.getDrawGraphics(Component.java:4115)
at scripps.rpg.Game.render(Game.java:86)
at scripps.rpg.Game.run(Game.java:107)
at java.lang.Thread.run(Thread.java:722)
我将完全相同的代码上传到运行Windows 7的笔记本电脑上并在那里工作,所以我知道它没有本地化到我的电脑上。这是与Stack-trace相关的代码。
错误是g = buffer.getDrawGraphics(); ,在我输入的代码>>>> <<<<<<围绕它
package scripps.rpg;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import scripps.states.GameState;
import scripps.states.MenuState;
import scripps.states.State;
//A separate instance of the game, Runnable allows it to be on a new thread
public class Game implements Runnable {
private Display display;
private int width;
private int height;
public String title;
private boolean running = false; //Used to control the game update
private Thread thread; //Creates a new thread object
private BufferStrategy buffer;
private Graphics g;
private BufferedImage test;
private SpriteSheet sheet;
//States of the game
private State gameState;
private State menuState;
//Input
public KeyManager keyManager;
//Camera
private GameCamera gameCamera;
//Handler
private Handler handler;
//Gives the game the height, width, and title
public Game(String title, int width, int height){
this.width = width;
this.height = height;
this.title = title;
keyManager = new KeyManager();
}
//initializes everything in the game
private void init(){
display = new Display(title,width, height);
display.getFrame().addKeyListener(keyManager);
Assets.init();
//Sets the camera with the default x and y values of 0,0
handler = new Handler(this);
gameCamera = new GameCamera(handler, 0,0);
handler = new Handler(this);
menuState = new MenuState(handler);
gameState = new GameState(handler);
State.setSate(gameState); //sets the state to the game state
}
//Updates the gameworld but does NOT render it
private void tick(){
keyManager.tick();
if(State.getState() != null){
State.getState().tick();
}
}
//Renders all the objects on the screen
private void render(){
buffer = display.getCanvas().getBufferStrategy(); //sets the display to the image set in the display
//Checks to see if there is already a way to determine the buffer, if not it creates one with 3 'screens'
if(buffer == null){
display.getCanvas().createBufferStrategy(3);
return;
}
// >>>>>>>>>>>>>>>> HERE <<<<<<<<<<<<<<<
g = buffer.getDrawGraphics(); // <<<<<<<<<<
// >>>>>>>>>>>>>>>> HERE <<<<<<<<<<<<<<<
//Clears the screen of any previous drawings
g.clearRect(0, 0, width, height);
//Ensures that there is currently a state to the game
if(State.getState() != null){
State.getState().render(g);
}
//Shows the screen and empties anything saved in the graphics variable
buffer.show();
g.dispose();
}
//Method required to allow the game to take place in a separate thread
public void run() {
init();
while(running == true){
tick();
render();
try { Thread.sleep(1000/60); } catch (InterruptedException e) {}
}
stop(); //Stops the thread
}
//Getter for the KeyManager class
public KeyManager getKeyManager(){
return keyManager;
}
public GameCamera getGameCamera(){
return gameCamera;
}
//Getters for the height and width
public int getWidth(){
return width;
}
public int getHeight(){
return height;
}
//Starts the new thread
public synchronized void start(){
//Doesn't execute code below as game is already running
if(running == true){
return;
}
running = true;
thread = new Thread(this);
thread.start();
}
//closes the new thread
public synchronized void stop(){
//Doesn't execute code below as game is not running
if(running ==false){
return;
}
running = false;
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
展示班级:
//Manages all the displays and windows
package scripps.rpg;
import java.awt.Canvas;
import java.awt.Dimension;
import javax.swing.JFrame;
public class Display {
private JFrame frame;
private Canvas canvas;
private String title;
private int width ;
private int height;
public Display(String title, int width, int height){
this.title = title;
this.width = width;
this.height = height;
createDisplay();
}
//Creates the JFrame display and sets up the specs of it
public void createDisplay(){
frame = new JFrame(title);
frame.setSize(width,height); //sets display to the height declared previously
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setLocationRelativeTo(null); //puts display at center of screen
frame.setVisible(true);
canvas = new Canvas();
canvas.setPreferredSize(new Dimension(width, height));
canvas.setMaximumSize(new Dimension(width, height));
canvas.setMinimumSize(new Dimension(width, height));
canvas.setFocusable(false);
frame.add(canvas);
frame.pack();
}
//Getter to get the private variables in this class, could also have made the variables public
public Canvas getCanvas(){
return canvas;
}
//Getter for JFrame
public JFrame getFrame(){
return frame;
}
}
如果有人能提供帮助,我们将不胜感激!