InternalFrameListener and JMenuItem problem

Hi there, I did the following (just an outline, I know, that program is not working.... :))

class A{

static JMenuItem m_saveMenu;

A(){

JMenuBar mB = new JMenuBar();

m_saveMenu = new JMenuItem("Save As...");

}

}

class B{

JInternalFrame iFrame = new JInternalFrame();

iFrame.addInternalFrameListener(new MyListener());

}

class MyListener extends InternalFrameAdapter{

public void internalFrameActivated(InternalFrameEvent e){

A.m_saveMenu.setEnabled(true);}

public void internalFrameDeactivated(InternalFrameEvent e){

A.m_saveMenu.setEnabled(false);

}

}

So if I (in my working program) create a new JInternalFrame and activate it I get the following error message:

Exception occurred during event dispatching:

java.lang.NullPointerException

why could that be?

Thx

Charly

[950 byte] By [JebeDiAH] at [2007-9-26 1:22:54]
# 1

hi,

I mean this is obvious: You didn't initialize the menu item, you just declared it! And the listener might be used before the constructor of class A is called.

You could do the following now:

static JMenuItem m_saveMenu=new JMenuItem("Save As...");

unless you are sure that the constructor of class A and with it the initialization of the menu item goes ahead.

best regards, Michael

Michael_Rudolf at 2007-6-29 1:00:37 > top of Java-index,Archived Forums,Java Programming...