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