代码说:线程中的异常" main"我尝试运行时java.lang.NullPointerException

时间:2015-05-23 20:53:14

标签: java multithreading jframe

它一直在说

线程中的异常" main"显示java.lang.NullPointerException     在jframe.game.JFrameGame.main(JFrameGame.java:29) Java结果:1

每当我尝试运行代码时,我都不知道为什么。我也一直在使用这个视频教程系列:https://www.youtube.com/watch?v=S_CIHzdpgE8&index=4&list=PLsc8wGybU2IZui4lAad9F8lFjdkn2ZdWD

package jframe.game;

import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JFrame;

public class JFrameGame extends Canvas implements Runnable{
    int height = 500;
    int width = 500;

    private JFrame frame;
    private Thread thread;
    private boolean running = false;

    public synchronized void start(){
        running = true;
        thread = new Thread((Runnable) this);


        thread.start();

    }

    public static void main(String[] args) {
        JFrameGame thegame = new JFrameGame();

        //frame = new JFrame();

        thegame.frame.setResizable(false);
        thegame.frame.setTitle("hello frame");
        thegame.frame.add(thegame);
        thegame.frame.pack();
        thegame.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        thegame.frame.setLocationRelativeTo(null);
        thegame.frame.setVisible(true);

        thegame.start();
    }

    public void run(){

        while(running == true){
            System.out.println("running the run method");
        }

    }

    public JFrameGame(){
        Dimension size = new Dimension(500, 500);
        setPreferredSize(size);

    }

}

0 个答案:

没有答案