0

I was trying to draw a simple rectangular on a JFrame but when I tried to draw it it gave me a NullPointerException. I could not find the problem because it is one of my codes that I already used. The object that is Null is a Graphics object that I got from the super class. Can someone help me with this?

Error:

Exception in thread "Thread-2" java.lang.NullPointerException
at com.daansander.game.Game.render(Game.java:83)
at com.daansander.game.Game.run(Game.java:76)
at java.lang.Thread.run(Thread.java:745)

Full Code:

package com.daansander.game;


import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferStrategy;

/**
 * Created by Daan on 12-9-2015.
 */
public class Game extends Canvas implements Runnable {

    public static final int WIDTH = 500;
    public static final int HEIGHT = WIDTH / 12 * 9;
    public static final int SCALE = 2;
    public static final String NAME = "2DGame";

    private JFrame frame;
    private volatile boolean running;

    public Game() {
        setMinimumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
        setMaximumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
        setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));

        frame = new JFrame(NAME);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new BorderLayout());

        frame.add(this, BorderLayout.CENTER);
        frame.pack();

        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

    }

    public static void main(String[] args) {
        new Game().start();
    }

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

    public synchronized void stop() {
    }

    @Override
    public void run() {
        long lastTime = System.currentTimeMillis();
        long current;
        long delta = 0;

        int frames = 0;
//        int ticks = 0;

        while (running) {
            current = System.currentTimeMillis();
            delta += current - lastTime;
            lastTime = current;
            frames++;

            if (delta > 1000) {
                delta -= 1000;
                System.out.println("FRAMES " + frames);
                frames = 0;

            }

            try {
                Thread.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            render();
        }
    }

    public void render() {
        BufferStrategy bs = getBufferStrategy();

        Graphics g = bs.getDrawGraphics(); // <----- Line of error

//        g.setColor(Color.black);
//        g.drawRect(0, 0, 5, 5);
//
//        g.dispose();
//        bs.show();
    }

    public void tick() {

    }

    public void init() {

    }
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
Daan Meijer
  • 143
  • 2
  • 12
  • 1
    http://docs.oracle.com/javase/7/docs/api/java/awt/Canvas.html#getBufferStrategy%28%29 – fabian Sep 12 '15 at 08:44
  • @fabian Thx Solved it – Daan Meijer Sep 12 '15 at 08:48
  • This will likely occur because the `Canvas` has not yet been realised on the screen (ie, attached to a native peer). You should make accommodations for it in your code. Make sure you also call `createBufferStrategy` on the `Canvas` before you call `getDrawGraphics` – MadProgrammer Sep 12 '15 at 08:48

1 Answers1

1

getBufferStrategy() is returning null. As fabian linked to above:

public BufferStrategy getBufferStrategy()

Returns the BufferStrategy used by this component. This method will return null if a BufferStrategy has not yet been created or has been disposed.

You need to create a BufferStrategy first.

Christian Ternus
  • 8,406
  • 24
  • 39