0

I am creating a very basic 2D game in java, and I wrote it on my desktop which is running Windows 7. I then exported my project to my Macbook and got the following error:

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)

I uploaded the exact same code onto a laptop running Windows 7 and it works there, so I know its not localized onto my computer. Here is the code that's relevant to the Stack-trace.

The error is g = buffer.getDrawGraphics(); , in the code I put >>>> <<<<<< around it

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

Display Class:

    //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;

}
}

If anyone would be able to help that would be greatly appreciated!

Leland22
  • 9
  • 5

0 Answers0