Exception error 2!

In the following code TankPanel calls method WasteWaterPanel() which creates a panel and adds to it a JComboBox where the initial item is 'SSVI'. I would then like to call the method SSVI_ModelParameters() if the selected item in the combo box is 'SSVI'.

The program compiles ok but seems to fail at the indicated line with a NullPointerException error but I cannot work out why. When the program is run without the if statement SSVI is the selected item in the JComboBox.

Code relating to Tank_JPanels[] has been ommitted for clarity.

Thanks

public JPanel Tank_JPanels[] =new JPanel[3];

public JPanel WasteWater_JPanel, SSVI_JPanel;

String Models[] ={"SSVI","GPSX"};

publicvoid SSVI_ModelParameters()

{

SSVI_JPanel =new JPanel();

SSVI_JPanel.setBorder(BorderFactory.createTitledBorder(

BorderFactory.createLineBorder(Color.gray),"SSVI Model Parameters"));

Tank_JPanels[2].add(SSVI_JPanel);

}

publicvoid WasteWaterPanel()

{

WasteWater_JPanel =new JPanel();

WasteWater_JPanel.setBorder(BorderFactory.createTitledBorder( BorderFactory.createLineBorder(Color.gray),"Waste Water Model"));

Tank_JPanels[2].add(WasteWater_JPanel);

JComboBox WasteWaterModel_JComboBox =new JComboBox(Models);

WasteWater_JPanel.add(WasteWaterModel_JComboBox);

}

public TankPanel(){

super();

WasteWaterPanel();

if( WasteWaterModel_JComboBox.getSelectedItem().equals("SSVI"))// Program seems to fail at this point

{

SSVI_ModelParameters();

}

}

[2458 byte] By [mmidea] at [2007-11-27 2:23:19]
# 1

> WasteWaterModel_JComboBox

I don't see where you declare this, but it is almost certainly null. I deduce that because in your WasteWaterPanel() method you have a local variable of the same name and you assign a JComboBox to it. I expect if you change the WasteWaterPanel() method so it assigns the JComboBox to the instance variable your problem should go away.

DrClapa at 2007-7-12 2:28:36 > top of Java-index,Java Essentials,New To Java...
# 2
Thanks, although WasteWaterModel_JComboBox was declared it must have been a local variable.
mmidea at 2007-7-12 2:28:36 > top of Java-index,Java Essentials,New To Java...