0

错误:“com.vipgamming.Frytree.Game.main(Game.java:47) 处的线程“主”java.lang.NullPointerException 中的异常”

我不是一个很好的程序员。只是说英语不好。

游戏.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();
    }
}

抱歉,我不明白如何发布代码。(不是英语。葡萄牙语)

4

2 回答 2

4

您需要在使用它之前实例化框架:

game.frame = new JFrame();
game.frame.setResizable(true);
...

你也可以把它放在构造函数中:

public Game() {
 this.frame = new JFrame();
 Dimension size = new Dimension(width * scale, height * scale);
 setPreferredSize(size);
}

如果中定义的设置main每次都相同,那么您可以将整个部分分解为构造函数,而不仅仅是创建一个空白。如果没有,你总是可以像这样重载构造函数:

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);
}

或让游戏创建设置

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);
}
于 2012-10-08T19:57:56.303 回答
2

Game.frame没有在任何地方初始化,因此是 NPE。最好将此帧初始化保存在单独的init方法中:

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);
}

(也更好地扩展JPanel而不是重量级 AWT Canvas

于 2012-10-08T19:58:09.613 回答