2013-11-21 37 views
0

正如標題,我有一個包含和與例如像(「複製」,新副本())幾個命令初始化一個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) 

,我會很感激,如果有人可以給我一些建議關於這個問題,我被困在這個* *由於時間:( 感謝

+0

嘗試SYSOUT調用命令之前有什麼在命令表。 – MightyPork

+0

它返回null我沒有指出 – TLR

+0

所以'System.out.println(commands)'給null?如果你發佈了更多的代碼,或者把它放在pastebin上,它可能會有所幫助。這裏沒有顯示很多重要的東西。 – MightyPork

回答

1

你正在創建用戶界面的兩倍,但只有兩個實例之一獲得commandsHashtable

第一用戶接口對象在這裏創建在main.java:

UI = new UserInterface(); 

第二個在該行的下一行後:

IHMObserver ihm = new IHMObserver(editor); 

IHMObserverUserInterface子類,因此當它建成時,第二個窗口在第一個窗口前打開。但是,您只能將您的命令設置爲UI實例,而不是ihm。但是當第二個窗口顯示在第一個窗口的前面時,它將捕獲鍵和鼠標事件,並且由於commandsnull,所以發生異常。

解決方法很簡單:只要跳過第一行(new UserInterface())和命令設置爲ihm對象,而不是:

editor = new EditorEngine(); 
IHMObserver ihm = new IHMObserver(editor); 

// commands creation comes here 

ihm.setCommands(commands); 
editor.registerObserver(ihm); 
+0

非常感謝,真的,我完全失去了這個混亂! – TLR