我想用键盘来唤起电脑的某些反应。所以我写了一个KeyEventDispatcher,但是我得到了一个NullPointerException。我无法说出异常发生在哪一行,因为编译器在任何情况下都指向同一行,无论是否存在代码或不存在代码。但是这个方法似乎被调用了,因为它打印出“test”。
以下是相关代码:
import java.util.*;
import java.awt.event.KeyEvent;
import java.awt.event.*;
import javax.swing.*;
import java.awt.KeyboardFocusManager;
import java.awt.KeyEventDispatcher;
public class Game extends JFrame
{
private JFrame frame;
public Game()
{
frame = new JFrame();
KeyEventDispatcher keyEventDispatcher = new InputKeyListener();
KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(keyEventDispatcher);
frame.setFocusable(true);
setFocusTraversalKeysEnabled(false);
setVisible(true);
}
private class InputKeyListener implements KeyEventDispatcher
{
private KeyEvent e;
@Override
public boolean dispatchKeyEvent(KeyEvent e) {
System.out.println(e);
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
System.out.println("test");
}
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
//do something
}
if (e.getKeyCode() == KeyEvent.VK_DOWN){
}
if (e.getKeyCode() == KeyEvent.VK_R){
}
return false;
}
}
这是堆栈跟踪:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Spiel$InputKeyListener.dispatchKeyEvent(Spiel.java:43)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:1020)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:899)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:727)
at java.awt.Component.dispatchEventImpl(Component.java:4731)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:729)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:688)
at java.awt.EventQueue$3.run(EventQueue.java:686)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:702)
at java.awt.EventQueue$4.run(EventQueue.java:700)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:699)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
编辑:解决 - 问题不是代码而是BlueJ,我被老师强迫使用。虽然我经常重新编译,但我不知道为什么会执行旧代码。我不得不重新启动BlueJ以使其执行新代码。但感谢大家帮助我:)。