在java游戏中绘制图像,给出空指针异常

时间:2011-12-05 14:33:26

标签: java image swing instance-variables paintcomponent

我正在尝试在我正在制作的游戏中加载图像。在我们在课堂上建立的小组赛后,我正在模仿它。在我的面板类中,我加载一个图像并将其绘制到背景,这是有效的,它找到我的图像并绘制它。

但是,无论我如何加载我放在我的宇宙飞船类中的图像并使用宇宙飞船的实例绘制它,它都会给我一个空指针异常。我猜测它永远不会找到图像,但是我把它放在源文件夹中的相同位置,以便成功地从中拉出背景图像。此外,如果我在我的gamescreen类中加载空间图像并将其绘制到面板,它可以正常工作。

这两个类的代码如下。我尝试用

加载图像
spaceship = ImageIO.read(new File("Background.png"));

以及:

Image img = Toolkit.getDefaultToolkit().createImage("spaceShip.png"); 

但是两种方式都获得相同的空指针。我迷路了,希望有人可以帮忙。

package temp;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class GameScreen extends JFrame {

    //Create an instance of a spaceship object
    SpaceShip spaceship;

    //Create an instance of a missile object
    Missile missile;

    //Create an instance of a launcher object
    Launcher launcher;

    //create an array that holds missile objects
    ArrayList<Missile> missileArray = new ArrayList<Missile>();

    //Creates an array to hold spaceship objects
    ArrayList<SpaceShip> enemyArray = new ArrayList<SpaceShip>();

    int lives = 3;

    public GameScreen() {
        //sets up the panel and the parent frame

        //sets the default specs of the JFrame and adds the panel to it
        this.setSize(600, 700);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);

        //Creates a panel object and adds it to the main frame
        panel panel1 = new panel(launcher, enemyArray, missileArray);
        this.add(panel1);
    }
}

class panel extends JPanel {
    //creates a new launcher object
    Launcher launcher;

    //creates a spaceship object
    SpaceShip space;

    //creates an arraylist of spaceship objects
    static ArrayList<SpaceShip> ships;

    //creates an arraylist of missile objects
    ArrayList<Missile> missiles;

    //loads images to draw 
    Image backGround;

    public panel(Launcher la, ArrayList<SpaceShip> ss, ArrayList<Missile> mi) {
        try {
            launcher = la;
            ships = ss;
            missiles = mi;

            backGround = ImageIO.read(new File("Background.png"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void paintComponent(Graphics g) {
        BufferedImage x = new BufferedImage(600, 700, BufferedImage.TYPE_INT_RGB);
        Graphics g2 = x.getGraphics();

        g2.drawImage(backGround, 0, 0, this);

        g2.drawImage(space.img, 0, 0, this);

        System.out.println(ships.size());

        g.drawImage(x, 0, 0, this);
    }
}

SpaceShip.java

import java.awt.Image;
import java.awt.Toolkit;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;

/**
 * @author WymoreJW
 */
class SpaceShip extends Character {

    Image img = Toolkit.getDefaultToolkit().createImage("spaceShip.png");

    public void SpaceShip() {

        this.health = 10;
        this.speed = 2;
    }
}

堆栈跟踪错误

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at missiledefense.panel.paintComponent(GameScreen.java:108)
    at javax.swing.JComponent.paint(JComponent.java:1029)
    at javax.swing.JComponent.paintChildren(JComponent.java:862)
    at javax.swing.JComponent.paint(JComponent.java:1038)
    at javax.swing.JComponent.paintChildren(JComponent.java:862)
    at javax.swing.JComponent.paint(JComponent.java:1038)
    at javax.swing.JLayeredPane.paint(JLayeredPane.java:567)
    at javax.swing.JComponent.paintChildren(JComponent.java:862)
    at javax.swing.JComponent.paintToOffscreen(JComponent.java:5131)

    at    javax.swing.RepaintManager$PaintManager.paintDoubleBuffered
(RepaintManager.java:1479)

    at javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1410)
    at javax.swing.RepaintManager.paint(RepaintManager.java:1224)
    at javax.swing.JComponent.paint(JComponent.java:1015)
    at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:21)
    at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:60)
    at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:97)
    at java.awt.Container.paint(Container.java:1780)
    at java.awt.Window.paint(Window.java:3375)
    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:796)
    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:713)
    at javax.swing.RepaintManager.seqPaintDirtyRegions(RepaintManager.java:693)
    at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run
(SystemEventQueueUtilities.java:125)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
    at java.awt.EventDispatchThread.pumpOneEventForFilters
(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy
(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

2 个答案:

答案 0 :(得分:3)

最大的问题是你的评论与代码(语义上)不一致:

//creates a spaceship object
SpaceShip space;

这不会创建实例。这将创建对实例的引用 - 但不是实例。它是null。您将它传递给(命名不佳的)panel类构造函数,并且表现得应该有一个值。但没有。

NPE,或至少其中一个:

g2.drawImage(space.img, 0, 0, this); // *foom*

至少在提供的代码中,space永远不会被初始化。

我还建议不要将所有这些都整理成一个文件。

答案 1 :(得分:0)

你的变量太空战,导弹和发射器都是空的。实例化一个你需要调用其构造函数的变量,例如

//Actually Create an instance of a spaceship object
SpaceShip spaceship = new SpaceShip();

//Actually Create an instance of a missile object
Missile missile = new Missile();

//Actually Create an instance of a launcher object
Launcher launcher = new Launcher();

如果您不这样做,那么当行panel panel1 = new panel(launcher, enemyArray, missileArray);运行时,面板构造函数将尝试设置launcher = la;。问题来了,因为la当前为空。

我还建议将您的面板类重命名为更具描述性的内容,或者至少更改为Panel(因为Java中的类应始终以大写字母开头)。

希望这有帮助。