当我想在私有类中访问它时,Hashtable是空的

时间:2013-11-21 20:20:04

标签: java nullpointerexception hashtable keylistener

在标题中,我有一个哈希表,其中包含并使用少量命令进行初始化,例如(“复制”,新复制()),例如

我有这个初始化哈希表的函数:

    public void setCommands(Hashtable<String, Command> h){
    this.commands = h;
    this.commands.get("write").execute();
}

包含我的keylistener的私有。为了解释这个过程,当我按下一个键时,它会从我的命令调用一个方法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;  
        }   
    }
}

所以,当我运行我的应用程序并按下一个键时,我在“command.get(”write“)行中有一个NullPointerExpection。执行()而我的哈希表不是空的!!

如果你愿意,这是stackTrace:

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)

如果有人能就这个问题给我一些建议,我会非常感激,因为几个小时我就坚持这个 * * 感谢

1 个答案:

答案 0 :(得分:1)

您正在创建两次用户界面,但两个实例中只有一个获得commands Hashtable

第一个用户界面对象在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);