I continue to get this error:
Exception in thread "main" java.lang.NullPointerException
at Main.GamePanel.<init>(GamePanel.java:106)
at Main.Game.main(Game.java:10)
This is the code:
Game Panel
<code>package Main;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.event.*;
import javax.swing.JPanel;
import GameState.GameStateManager;
public class GamePanel extends JPanel implements Runnable, KeyListener{
//dimensions
public static final int WIDTH = 320;
public static final int HEIGHT = 240;
public static final int SCALE = 2;
//game thread
private Thread thread;
private boolean running;
private int FPS = 60;
private long targetTime = 1000 / FPS;
//image
private BufferedImage image;
private Graphics2D g;
//game state manager
private GameStateManager gsm;
public GamePanel() {
super();
setPreferredSize (new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
setFocusable(true);
requestFocus();
}
public void addNotify() {
super.addNotify();
if(thread == null) {
thread = new Thread(this);
addKeyListener(this);
thread.start();
}
}
private void init() {
image = new BufferedImage(
WIDTH, HEIGHT,
BufferedImage.TYPE_INT_RGB
);
g = (Graphics2D) image.getGraphics();
running = true;
gsm = new GameStateManager();
}
public void run() {
init();
long start;
long elapsed;
long wait;
//game loop
while(running) {
start = System.nanoTime();
update();
draw();
drawToScreen();
elapsed = System.nanoTime() - start;
wait = targetTime - elapsed / 1000000;
try {
Thread.sleep(wait);
}
catch(Exception e) {
e.printStackTrace();
}
}
}
private void update() {
gsm.update();
}
private void draw() {
gsm.draw();
}
private void drawToScreen() {} {
Graphics g2 = getGraphics();
g2.drawImage(image, 0, 0, null);
g2.dispose();
}
public void keyTyped(KeyEvent key) {}
public void keyPressed(KeyEvent key) {
gsm.keyPressed(key.getKeyCode());
}
public void keyReleased(KeyEvent key) {
gsm.keyReleased(key.getKeyCode());
}
}
</code>
Game
<code>package Main;
import javax.swing.JFrame;
public class Game {
public static void main (String[] args) {
JFrame window = new JFrame("Game");
window.setContentPane(new GamePanel());
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLO SE);
window.setResizable(false);
window.pack();
window.setVisible(true);
}
}
</code>
any help will be greatly appreciated