TLR
TLR

Reputation: 587

Hashtable is empty when I want to access it in a private class

As in the title, I have a hashtable which contains and which is initialized with few commands like ("Copy",new Copy()) for example

I have this function which initialize the hashtable :

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

And a private which contains my keylistener. To explain the process, when I press a key it call a methode execute() from my command which enter a key in a buffer. The latter one is displayed in my user interface.

Here is my private class for my 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;  
        }   
    }
}

So, when I run my app and press a Key I have a NullPointerExpection at the line "command.get("write").execute() whereas my hashtable is not empty !!

Here is the stackTrace if you want :

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)

I would be very thankful if someone could give me some advices about this problem, I'm stuck on this ** since hours :( Thanks

Upvotes: 0

Views: 155

Answers (1)

isnot2bad
isnot2bad

Reputation: 24454

You are creating your user interface twice, but only one of the two instances get the commands Hashtable!

The first user interface object is created here in main.java:

UI = new UserInterface();

The second one in the line after the next line:

IHMObserver ihm = new IHMObserver(editor);

IHMObserver is a subclass of UserInterface, so when it's constructed, a second window opens in front of the first. But you only set your commands to the UI instance, not to ihm. But as the second window is shown in front of the first one, it will catch the key- and mouse-events and as commands is null there, an exception occures.

The solution is simple: Just skip the first line (new UserInterface()) and set the commands to the ihm object instead:

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

// commands creation comes here

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

Upvotes: 1

Related Questions