-1

I am getting an error: java.lang.NullPointerException on Graphics g. this is my code:

package main;
import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import javax.swing.JFrame;
public class main implements Runnable {
public static final int WIDTH = 1024;
public static final int HEIGHT = 1024;
public static final String TITLE = "platformer";
public boolean running = false;
static main home = new main();
public Graphics g;
public static Canvas canvas = new Canvas();
public static void main(String[] args) {
    home.display();
    home.start();
}
public void display(){
    JFrame frame = new JFrame(TITLE);
    frame.setSize(WIDTH, HEIGHT);
    frame.setResizable(false);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    canvas.setPreferredSize(new Dimension(WIDTH, HEIGHT));
    frame.add(canvas);
}
BufferStrategy bs;
public void render(){
    bs = main.getCanvas().getBufferStrategy();
    if(bs == null){
        main.getCanvas().createBufferStrategy(3);
    }
    g = bs.getDrawGraphics();
    g.fillRect(0, 0, WIDTH, HEIGHT);
    g.dispose();
    bs.show();
}
public void tick(){

}
public void run(){
    long lastTime = System.nanoTime();
    final double amountofticks = 60;
    double ns = 1000000000/amountofticks;
    double delta = 0;
    double time = 0;
    int frames = 0;
    while(running){
        long now = System.nanoTime();
        delta += (now-lastTime)/ns;
        time += (now-lastTime);
        lastTime = now;
        if(delta>=1){
            home.tick();
            home.render();
            delta--;
            frames++;
        }
        if(time>1000000000){
            System.out.println(frames);
            time = 0;
            frames = 0;
        }
    }
    stop();
}
private Thread thread;
public void start(){
    running = true;
    thread = new Thread(this);
    thread.start();
}
public static Canvas getCanvas(){
    return canvas;
}
public void stop(){
    try {
        thread.join();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
}

i am getting the error message: Exception in thread "Thread-2" java.lang.NullPointerException at main.main.render(main.java:37) at main.main.run(main.java:59) at java.lang.Thread.run(Unknown Source) line 37 is g = bs.getDrawGraphics(); I think that the graphics is null for some reason. i have tried to make this code for a long time

Eli Holst
  • 1
  • 2
  • 2
    For sanity purposes, please do not define packages called `main`, or classes called `main`, as it becomes remarkbaly difficult to interpret. – Compass Oct 19 '16 at 20:53
  • 1
    Please learn to indent your code properly. If it's indented right in your source file, then when you copy/paste into Stack Overflow, select all of it and press CTRL+K – Nic Oct 19 '16 at 20:53

1 Answers1

1

Change line

main.getCanvas().createBufferStrategy(3);

to

bs = main.getCanvas().createBufferStrategy(3); 
Rob Gorman
  • 3,502
  • 5
  • 28
  • 45