线程“AWT-EventQueue-0”塔防的例外情况

时间:2013-05-28 18:11:18

标签: java eclipse awt

所以我正在制作一个塔防游戏,当我运行代码时,我想在那里做一个网格显示,但我得到的是一个空白的窗口,这在我的控制台中:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at Room.draw(Room.java:41)
    at Screen.paintComponent(Screen.java:42)
    at javax.swing.JComponent.paint(Unknown Source)
    at javax.swing.JComponent.paintToOffscreen(Unknown Source)
    at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
    at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
    at javax.swing.RepaintManager.paint(Unknown Source)
    at javax.swing.JComponent._paintImmediately(Unknown Source)
    at javax.swing.JComponent.paintImmediately(Unknown Source)
    at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
    at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
    at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
    at javax.swing.RepaintManager.access$700(Unknown Source)
    at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

这是我的代码:

帧类:

import javax.swing.*;
import java.awt.*;

public class Frame extends JFrame {

    public static String title = "Tower Defence";
    public static Dimension size = new Dimension(700,550);

    public Frame()
    {
        setTitle(title);
        setSize(size);
        setResizable(false);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        init();

    }

    public void init()
    {
        setLayout (new GridLayout(1, 1, 0, 0));

        Screen screen = new Screen();
        add(screen);
        setVisible(true);
    }

    public static void main(String args[])
    {
        Frame frame = new Frame();

    }

}

屏幕等级:

import java.awt.Graphics;

import javax.swing.*;


public class Screen extends JPanel implements Runnable
{

    public Thread thread = new Thread(this);


    public static int myWidth, myHeight;

    public static boolean isFirst = true;

    public static Room room;

    public Screen()
    {
        thread.start();
    }

    public void define()
    {
        room = new Room();
    }



    public void paintComponent (Graphics g)
    {
        if(isFirst)
        {
            myWidth = getWidth();
            myHeight = getHeight();
            define();
            isFirst = false;
        }

        g.clearRect(0,0, getWidth(), getHeight());
        room.draw(g); //Drawing the room.
    }

    public void run()
    {
        while(true)
        {
            if(!isFirst)
            {
                room.physics();
            }

            repaint();


            try
            {
                Thread.sleep(1);

            } catch(Exception e){}
        }
    }
}

房间等级:

import java.awt.*;


public class Room 
{
public int worldWidth = 12;
public int worldHeight = 8;
public int blockSize = 52;

public Block[][] block; 

    public Room()
    {       

    }

    public void define()
{
        block = new Block[worldWidth][worldHeight];

    for(int y=0;y<block.length;y++) 
    {
        for(int x=0;x<block[0].length;x++)
        {
            block[y][x] = new Block(((Screen.myWidth/2)) - ((worldWidth*blockSize/2)) + (x * blockSize), y * blockSize, blockSize, blockSize, 0, 0);
        }
    }
}


    public void physics()
    {


    }



    public void draw(Graphics g)
    {
        for(int y=0;y<block.length;y++) 
        {
            for(int x=0;x<block[0].length;x++)
            {
                block[y][x].draw(g);
            }
        }
    }   
}

Block Class:

import java.awt.*;

public class Block extends Rectangle
{
    public int groundID;
    public int airID;

    public Block(int x, int y, int width, int height, int groundID, int airID)
    {
        setBounds(x,y,width,height);
        this.groundID = groundID;
        this.airID = airID;
    }

    public void draw(Graphics g)
    {
        g.drawRect(x, y, width, height);
    }
}

提前致谢!

1 个答案:

答案 0 :(得分:1)

Room课程中,block变量(或其中一个元素)为null。因为错误发生在这一行:

    public void draw(Graphics g)
    {
>       for(int y=0;y<block.length;y++) 
        {
            for(int x=0;x<block[0].length;x++)
            {
                block[y][x].draw(g);
            }
        }
    }   

我要说在调用define()方法之前,您还没有调用block(似乎初始化draw()变量)。