I'm sorry if this isn't the right place for this post, but I don't know where to go.
I'm trying to make a simple game loop but it's already failing
package main; import javax.swing.JPanel; import java.awt.*; import java.awt.image.*; public class Logic extends JPanel implements Runnable { /** * */ private static final long serialVersionUID = 1L; // Main Game Stuff private int gWidth = 800; private int gHeight = 640; private int scale = 2; private int fps = 30; private boolean running; private Thread thread; private BufferedImage image; private Graphics2D g; public Logic(){ super(); setPreferredSize(new Dimension(gWidth, gHeight)); setFocusable(true); requestFocus(); } public void addNotify(){ if(thread==null) { thread=new Thread(this); thread.start(); } } public void init() { image = new BufferedImage( gWidth, gHeight, BufferedImage.TYPE_INT_RGB ); g = (Graphics2D) image.getGraphics(); System.out.println("Hello!"); running = true; } private void update() //main logic { //do stuff } private void render(Graphics2D g) { g.setColor(Color.BLUE); g.fillRect(0, 0, gWidth, gHeight); } private void PaintCanvas(){ Graphics g2 = getGraphics(); g2.drawImage(image, 0, 0, null); g2.dispose(); } public void run() { init(); int loops = 0; long startTime = System.nanoTime(); float rate = 1000/fps; long lastTime = startTime; float waitTime = rate; while(running) // game loop { long currTime = System.nanoTime(); if(currTime > (lastTime + waitTime)) { waitTime = rate; if((currTime - lastTime) > rate) waitTime = currTime - lastTime; //-- Paint Canvas --// PaintCanvas(); //-- LOGIC -- // update(); //////////////// //-- RENDER --// if(loops % 2 == 0)// draw every second frame { render(g); } //////////////// loops++; lastTime = currTime; } } } } I keep getting this error:
Exception in thread "Thread-2" java.lang.NullPointerException at main.Logic.PaintCanvas(Logic.java:67) at main.Logic.run(Logic.java:93) at java.lang.Thread.run(Unknown Source) Why is the image null? I can't for the life of me figure it out. I already declared it in the init() function.