Error:"Exception in thread "main" java.lang.NullPointerException at com.vipgamming.Frytree.Game.main(Game.java:47)"
I am not a very good programmer. Just saying and bad Inglish.
Game.java:
package com.vipgamming.Frytree;
import java.awt.Canvas;
import java.awt.Dimension;
import javax.swing.JFrame;
public class Game extends Canvas implements Runnable {
private static final long serialVersionUID = 1L;
public static int width = 300;
public static int height = width /16 * 9;
public static int scale = 3;
private Thread thread;
private JFrame frame;
private boolean running = false;
public Game() {
Dimension size = new Dimension(width * scale, height * scale);
setPreferredSize(size);
}
public synchronized void start() {
running = true;
thread = new Thread(this, "Display");
thread.start();
}
public synchronized void stop() {
running = false;
try {
thread.join();
}catch (InterruptedException e) {
e.printStackTrace();
}
}
public void run() {
while (running) {
System.out.println("FryTree...Loading...");
}
}
public static void main(String [] args) {
Game game = new Game();
game.frame.setResizable(true);
game.frame.setTitle("Frytree");
game.frame.add(game);
game.frame.pack();
game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
game.frame.setLocationRelativeTo(null);
game.frame.setVisible(true);
game.start();
}
}
Sorry I didnt understand how to post code.(not Inglish.Portuguese)
You need to instantiate frame before using it:
game.frame = new JFrame();
game.frame.setResizable(true);
...
You could also put this in the constructor:
public Game() {
this.frame = new JFrame();
Dimension size = new Dimension(width * scale, height * scale);
setPreferredSize(size);
}
If the settings defined in main
are going to be the same every time then you could break that whole piece out into the constructor instead of just creating a blank one. If not, you could always overload the constructor like this:
public Game() {//base constructor
this.frame = new JFrame();
Dimension size = new Dimension(width * scale, height * scale);
setPreferredSize(size);
}
public Game(JFrame jframe)//injected frame constructor
{
this.frame = jframe;
Dimension size = new Dimension(width * scale, height * scale);
setPreferredSize(size);
}
or have Game create the settings
public Game()
{
this.ConstructFrame();
Dimension size = new Dimension(width * scale, height * scale);
setPreferredSize(size);
}
private void ConstructFrame()
{
this.frame = new JFrame();
this.frame.setResizable(true);
this.frame.setTitle("Frytree");
this.frame.add(game);
this.frame.pack();
this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.frame.setLocationRelativeTo(null);
this.frame.setVisible(true);
}
Game.frame
is not initialised anywhere hence the NPE. Better to keep this frame initialization in a separate init
method:
private void init() {
frame = new JFrame();
frame.setSize(400, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(true);
frame.setTitle("Frytree");
frame.add(this);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
(Also better to extend JPanel
instead of heavyweight AWT Canvas
)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.