eclipse:线程“AWT-EventQueue-0”中的异常java.lang.NullPointerException

时间:2012-07-23 15:26:24

标签: java eclipse nullpointerexception awt

(我是java的新手所以请不要让你的答案TOOOOOOO CONFUSING !!!!:P。) 我刚刚创建了一个使用eclipse的游戏,我真的很想制作一个有趣的游戏!我看了一些帮助我创建它的教程。事情是我无法在游戏中移动。我真的很感激帮助! 这是我在游戏中尝试使用wasd时收到的错误:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at StickFireBasic.KeyAdapt.keyReleased(KeyAdapt.java:19)
    at java.awt.Component.processKeyEvent(Unknown Source)
    at javax.swing.JComponent.processKeyEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.KeyboardFocusManager.redispatchEvent(Unknown Source)
    at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source)
    at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown Source)
    at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source)
    at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(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.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.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)
My class files are: Main, GameFrame, Entity, Player , KeyAdapt.

主:

package StickFireBasic;

import javax.swing.JFrame;

public class Main {
    public static void main(String[] args) {
        JFrame frame = new JFrame("StickFire");
        frame.setSize(800, 600);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        frame.setResizable(true);
        frame.add(new GameFrame());
        frame.setVisible(true);
    }

}

GameFrame:

package StickFireBasic;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.Timer;

public class GameFrame  extends JPanel implements ActionListener{

    Timer mainTimer;
    Player player;

    public GameFrame() {
        setFocusable(true);

        player = new Player(400, 300);
        addKeyListener(new KeyAdapt(player));
        mainTimer = new Timer(10, this);
        mainTimer.start();
    }

    public void paint(Graphics g) {
        super.paint(g);
        Graphics2D g2d = (Graphics2D) g;
        player.draw(g2d);

    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        player.update();
        repaint();
    }
}

实体:

package StickFireBasic;

import java.awt.Graphics2D;

public class Entity {

    int x, y;

    public Entity(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public void update() {

    }

    public void draw(Graphics2D g2d) {

    }
}

播放器:

package StickFireBasic;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.KeyEvent;

import javax.swing.ImageIcon;

public class Player extends Entity{

    int velX = 0, velY = 0;

    public Player(int x, int y) {
        super(x, y);

    }

    public void update() {
        y += velY;
        x += velX;
    }

    public void draw(Graphics2D g2d) {
        g2d.drawImage(getPlayerImg(), x, y, null);
    }

    public Image getPlayerImg() {


        ImageIcon ic = new ImageIcon("C:/img/stickguy.png/");
        return ic.getImage();
    }

    public void keyPressed(KeyEvent e) {
        int key = e.getKeyCode();

        if (key == KeyEvent.VK_W) {
            velY = -2;
        } else if (key == KeyEvent.VK_S) {
            velY = 2;
        } else if (key == KeyEvent.VK_A) {
            velX = -2;
        } else if (key == KeyEvent.VK_D) {
            velX = 2;
        }

    }

    public void keyReleased(KeyEvent e) {
        int key = e.getKeyCode();

        if (key == KeyEvent.VK_W) {
            velY = 0;
        } else if (key == KeyEvent.VK_S) {
            velY = 0;
        } else if (key == KeyEvent.VK_A) {
            velX = 0;
        } else if (key == KeyEvent.VK_D) {
            velX = 0;
        }

    }

}

KeyAdapt:

package StickFireBasic;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class KeyAdapt  extends KeyAdapter {

    Player p;

    public KeyAdapt(Player player) {

    }

    public void keyPressed(KeyEvent e) {
        p.keyPressed(e);
    }

    public void keyReleased(KeyEvent e) {
        p.keyReleased(e);
    }

}

1 个答案:

答案 0 :(得分:0)

嗯,这对我来说不对:

Player p;

public KeyAdapt(Player player) {

}

你不应该在某个地方为p分配一个值吗?像构造函数一样?目前,您完全忽略了构造函数参数。

否则,它将始终具有空值 - 因此当您在处理程序中取消引用它时,它将抛出NullPointerException。你还应该将它重命名为更具描述性,很可能重命名类本身,并将其设为私有和最终:

public class PlayerKeyAdapter extends KeyAdapter {
    private final Player player;

    public PlayerKeyAdapter(Player player) {
        this.player = player;
    }

    public void keyPressed(KeyEvent e) {
        player.keyPressed(e);
    }

    public void keyReleased(KeyEvent e) {
        player.keyReleased(e);
    }
}