Java错误“线程中的异常”主“java.lang.NullPointerException”

时间:2012-10-08 19:55:19

标签: java

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

我不是一个非常优秀的程序员。只是说英语和英语。

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

抱歉,我不明白如何发布代码。(不是Inglish.Portuguese)

2 个答案:

答案 0 :(得分: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);
}

答案 1 :(得分:2)

因为NPE,

Game.frame没有在任何地方初始化。最好将此帧初始化保持在单独的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