Search code examples
javadata-structuresgraphicsbufferstrategy

java - BufferStrategy is not creating stategy? (nullpointerexception)


I decided to start understanding BufferStrategy for my graphics. I'm not sure if using my jframe in a static form is what's cause this, but it looks alright to me. What am I missing?

Main.java

package Main;

import java.awt.Toolkit;


public class Main implements Runnable {

    private Thread gameThread;
    private Game game;
    private boolean running = false;
    public static ClientFrame frame;
    public static Toolkit kit;
    public static int WIDTH = 300, HEIGHT = WIDTH*16/9, SCALE = 3;

    public Main() {
        game = new Game();
        frame = new ClientFrame(game);
        kit = frame.getToolkit();

        frame.setVisible(true);
        start();
    }
    public synchronized void start() {
        running = true;
        gameThread = new Thread(this);

        gameThread.start();
    }
    public synchronized void stop() {
        running = false;

        gameThread.interrupt();
    }

    public void run() {
        long startTime = System.nanoTime();
        double nanoSec = 1000000000/60;
        double delta = 0;

        while(running) {
            long currentTime = System.nanoTime();
            delta += (currentTime - startTime)/nanoSec;

            while(delta >= 1) {
                game.update();

                delta--;
            }
            game.render();

            startTime = currentTime;
        }

    }

    public static void main(String[] args) {
        new Main();
    }

}

Game.java

package Main;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;

import javax.swing.JPanel;

public class Game extends JPanel {

    Player player;

    int tileArea = 32;

    public Game() {
        player = new Player();
        setPreferredSize(new Dimension(Main.WIDTH*Main.SCALE, Main.HEIGHT*Main.SCALE));

    }

    public void update() {

    }

    public void render() {
        BufferStrategy bs = Main.frame.getBufferStrategy();
        if(bs == null)
            Main.frame.createBufferStrategy(3);

        Graphics g = bs.getDrawGraphics();

        player.paint(g);

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

}

My Player.java only contains one method:

public void paint(Graphics g) {
    g.fillRect(25, 25, 50, 50);
}

ERROR:

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

Solution

  • You do not try to get the buffer strategy after you have created it:

    BufferStrategy bs = Main.frame.getBufferStrategy();
    if(bs == null)
        Main.frame.createBufferStrategy(3);
    
    //  if bs was null before, it still is null
    Graphics g = bs.getDrawGraphics();
    

    Also note the point by @MadProgrammer that the buffer strategy belongs to a different component. If your intent is to create an AWT game (I'd recommend swing instead), you should probably use Canvas, and its createBufferStrategy().