正如標題,我有一個包含和與例如像(「複製」,新副本())幾個命令初始化一個Hashtable哈希表是空的,當我想訪問它的私有類
我有這個函數初始化哈希表:它包含我的KeyListener
public void setCommands(Hashtable<String, Command> h){
this.commands = h;
this.commands.get("write").execute();
}
和私人。爲了解釋這個過程,當我按下一個鍵時,它會從我的命令中調用一個方法execute(),它在緩衝區中輸入一個鍵。後者顯示在我的用戶界面中。
這是我爲我的KeyListener私有類:
private class KeyListener implements java.awt.event.KeyListener
{
@Override
public void keyPressed(KeyEvent e) {
switch(e.getKeyChar())
{
case KeyEvent.VK_BACK_SPACE :
commands.get("delete").execute();
break;
default :
lastChar = String.valueOf(e.getKeyChar());
commands.get("write").execute();
break;
}
}
}
所以,當我跑我的應用程序,並按下一個鍵我都不得不線NullPointerExpection「command.get(」寫「)執行(。 ),而我的哈希表不是空的!
這裏是堆棧跟蹤,如果你想:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at invoker.UserInterface$KeyListener.keyPressed(UserInterface.java:149)
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$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)
,我會很感激,如果有人可以給我一些建議關於這個問題,我被困在這個* *由於時間:( 感謝
嘗試SYSOUT調用命令之前有什麼在命令表。 – MightyPork
它返回null我沒有指出 – TLR
所以'System.out.println(commands)'給null?如果你發佈了更多的代碼,或者把它放在pastebin上,它可能會有所幫助。這裏沒有顯示很多重要的東西。 – MightyPork