I was following a tutorial on making a simple game with Java, but for some reason my code stopped working for no reason that I can find. This is Window class that I was using.
package com.tutorial.main;
import java.awt.Canvas;
import java.awt.Dimension;
import javax.swing.JFrame;
public class Window extends Canvas{
private static final long serialVersionUID = -240840600533728354L;
public Window(int width, int height, String title, Game game){
JFrame frame = new JFrame(title); // line 13
frame.setPreferredSize(new Dimension(width,height));
frame.setMaximumSize(new Dimension(width,height));
frame.setMinimumSize(new Dimension(width,height));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.add(game);
frame.setVisible(true);
game.start();
}
}
Yesterday it worked fine. Today, when I reopened Eclipse, added some more code and tried running it I get a NullPointer Exception.
Exception in thread "main" java.lang.NullPointerException
at java.awt.Window.init(Window.java:497)
at java.awt.Window.<init>(Window.java:537)
at java.awt.Frame.<init>(Frame.java:420)
at javax.swing.JFrame.<init>(JFrame.java:233)
at com.tutorial.main.Window.<init>(Window.java:13)
at com.tutorial.main.Game.<init>(Game.java:26)
at com.tutorial.main.Game.main(Game.java:105)
I did not change anything in the Window class, nor the game class so I have no idea what is causing this. This is the line in Game.java that calls the window:
new Window(WIDTH, HEIGHT, "Game", this); // line 26