2 classes, boolean variable, actionPerformed; NullPointerException
I have 2 classes; in one of them I set up a variable (boolean playerReady) and would like to change it in the other class; unfortunately I get a NullPointerException and can't find the cause:
publicclass PMgmtextends JComponentimplements MouseListener{
publicboolean playerReady =false;
publicvoid mouseClicked(MouseEvent evt){
if (SwingUtilities.isLeftMouseButton(evt)
playerReady =true;
(...)
publicclass TPanelextends JComponentimplements ActionListener{
public PMgmt pm;
publicvoid actionPerformed(ActionEvent e){
// Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException:
pm.playerReady =false;
}
(...)
Thanks!
[1535 byte] By [
SFLa] at [2007-11-27 6:48:47]

> Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
The pointer in question is probably pm. The TPanel instance which throws this exception has not initialised the pm variable.
If this is the case there are two ways you can go:
(1) Provide TPanel with a constructor which takes a PMgmt argument. Have the constructor initialise pm so that when actionPerformed() is invoked pm will have the correct (non null) value.
A variation on this is to provide the TPanel class with a setter method that sets the value of pm.
(2) Make the PMgmt instance itself an action listener on whatever component the TPanel was listening to. That way PMgmt will be responsible itself for setting the value of playerReady.