1

基本上我是一个初学者程序员,我对 C#、C++ 了解一点,现在我正在学习 Java 来构建 Android 应用程序。我正在从brandonio生产的youtube学习java,非常有帮助。http://www.youtube.com/user/BrandonioProductions?feature=watch

这是完整的代码:

import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;

public class KeyListenerApp 
extends Applet
implements KeyListener{
private Rectangle rect; //The rectangle that moving
private ArrayList<Integer> keysDown;
public void init()
{
    rect = new Rectangle(0,0,50,50);
    this.addKeyListener(this);
}
public void paint(Graphics g)
{
    setSize(500,500);
    Graphics2D g2 = (Graphics2D)g;
    g2.fill(rect);
}
@Override
public void keyPressed(KeyEvent e) {
    if(keysDown.contains(new Integer(e.getKeyCode())))
    {
        keysDown.add(new Integer(e.getKeyCode()));
    }
    moveRect();
}
@Override
public void keyReleased(KeyEvent e) {
    keysDown.remove(new Integer(e.getKeyCode()));
}
public void moveRect()
{
    int x = rect.x;
    int y = rect.y;
    if (keysDown.contains(KeyEvent.VK_UP))
        y -= 2;
    if (keysDown.contains(KeyEvent.VK_DOWN))
        y += 2;
    if (keysDown.contains(KeyEvent.VK_LEFT))
        x -= 2;
    if (keysDown.contains(KeyEvent.VK_RIGHT))
        x += 2;
    rect.setLocation(x, y);
    repaint();
}
@Override
public void keyTyped(KeyEvent e) {
}
}

这是完整的错误:

Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
    at KeyListenerApp.keyPressed(KeyListenerApp.java:34)
    at java.awt.Component.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.Component.dispatchEvent(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.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)
Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
    at KeyListenerApp.keyReleased(KeyListenerApp.java:44)
    at java.awt.Component.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.Component.dispatchEvent(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.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)

很抱歉一开始没有在这里发布代码,我认为如果它与问题分开的话,对你来说会更舒服。

谢谢

这是工作代码:

import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;


public class KeyListenerApp 
extends Applet
implements KeyListener{

private Rectangle rect; //The rectangle that moving

private ArrayList<Integer> keysDown;

public void init()
{
    rect = new Rectangle(0,0,50,50);
    this.addKeyListener(this);
    keysDown = new ArrayList<Integer>();
}

public void paint(Graphics g)
{
    setSize(500,500);
    Graphics2D g2 = (Graphics2D)g;
    g2.fill(rect);
}

@Override
public void keyPressed(KeyEvent e) {

    if(keysDown.contains(new Integer(e.getKeyCode())) == false)
    {
        keysDown.add(new Integer(e.getKeyCode()));
    }

    moveRect();
}

@Override
public void keyReleased(KeyEvent e) {
    keysDown.remove(new Integer(e.getKeyCode()));

}

public void moveRect()
{
    int x = rect.x;
    int y = rect.y;

    if (keysDown.contains(KeyEvent.VK_UP))
        y -= 2;

    if (keysDown.contains(KeyEvent.VK_DOWN))
        y += 2;

    if (keysDown.contains(KeyEvent.VK_LEFT))
        x -= 2;

    if (keysDown.contains(KeyEvent.VK_RIGHT))
        x += 2;

    rect.setLocation(x, y);
    repaint();
}

@Override
public void keyTyped(KeyEvent e) {


}

}

是什么导致错误:

if(keysDown.contains(new Integer(e.getKeyCode())) == false)
{
    keysDown.add(new Integer(e.getKeyCode()));
}

在我修复它之前,这段代码是这样的:

if(keysDown.contains(new Integer(e.getKeyCode())))
{
    keysDown.add(new Integer(e.getKeyCode()));
}

发生错误是因为我在代码中说:如果有东西存在则添加他,并且假设只有当他不存在时才添加他。

4

2 回答 2

0
private ArrayList<Integer> keysDown;

您永远不会创建此对象的实例,因此NullPointerException.

于 2013-05-03T15:42:25.593 回答
0

keysDown 为空!它还没有被初始化。你应该这样做:

keysDown = new ArrayList<Integer>();

在使用它之前以某种方法,并用 ascii 键填充您的列表。

于 2013-05-03T15:44:23.737 回答