JComboBox null pointer Exception error
I can not figure out this JComboBox null pointer exception error. It happens when I attempt to use getSelectedItem() inside an ActionListener. The error doesn't actually initiate until I select an item with the combo box on the GUI when I run it. I don't understand why its null and after trying to figure it out for the past couple of hours I am stumped. where the combo box is created and initialized is highlighted in red. So is where the error is coming from.
Code :
import java.awt.event.*;
public class GUIController implements ActionListener
{
//members
private GUIView view;
public GUIController()
{
view = new GUIView(this);
}
public static void main(String [] args)
{
//GUIView one = new GUIView();
GUIController control = new GUIController();
}
public void actionPerformed (ActionEvent evt)
{
//Clear all text input
if (evt.getActionCommand().equals("Clear Input"))
{
view.getNameTxt().setText("");
view.getSocialSecurityTxt().setText("");
view.getMaritalStatusTxt().setText("");
view.getSalaryTxt().setText("");
view.getBossNameTxt().setText("");
view.getDepartmentTxt().setText("");
view.getTitleTxt().setText("");
view.getBonusTxt().setText("");
}
[COLOR="Red"] //null exception error occurs here
String input = (String)view.getEmployeeTypeBox().getSelectedItem();[/COLOR]
if (input.equals("Worker"))
view.getBonusTxt().setEditable(false);
}
}
And the GUIView Class.......................
Code :
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.AncestorListener;
public class GUIView extends JFrame
{
//members
private ActionListener controller;
//size of Frame
private static final int LENGTH = 700;
private static final int WIDTH = 400;
//size of input field bar
private static final int BAR_SIZE = 10;
//the final JPanel to be put onto the JFrame
// private JPanel mainPanel;
private JList employeeList;
private String input;
//=========For construction of input panel (West region of GUI)===
//panels for construction of west GUI portion
private JPanel gridInputPanel;
private JPanel inputPanel;
//Text fields for inputPanel
private JTextField txtName;
private JTextField txtSocialSecurity;
private JTextField txtMaritalStatus;
private JTextField txtSalary;
private JTextField txtBossName;
private JTextField txtDepartment;
private JTextField txtTitle;
private JTextField txtBonus;
//labels for inputPanel
private JLabel lblName;
private JLabel lblSocialSecurity;
private JLabel lblMaritalStatus;
private JLabel lblSalary;
private JLabel lblBossName;
private JLabel lblDepartment;
private JLabel lblTitle;
private JLabel lblBonus;
//JButtons
private JButton addEmployee;
private JButton clearInputButton;
[COLOR="Red"] //combo box is declared here
private JComboBox employeeTypeBox;
private String[] employeeTypes = {" ","Employee", "Manager", "Worker"};
[/COLOR]
//=========For construction of RIGTH region of GUI===
private JPanel gridEmploySelection;
//buttons
private JButton sortListButton;
private JButton clearListButton;
//may want to add another argument for different actions
GUIView(ActionListener ctr)
{
super("Employee GUI");
controller = ctr;
setSize(LENGTH, WIDTH);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(1,4));
//build the panels
buildInputPanel();
//for crdating sort button and list
configureListComponents();
buildSortPanel();
//Add input Panel to frame
add(gridInputPanel, BorderLayout.WEST);
//Add listPanel to fram
employeeList.setVisible(true);
add(employeeList, BorderLayout.CENTER);
add(gridEmploySelection, BorderLayout.EAST);
setVisible(true);
}
private void configureListComponents()
{
employeeList = new JList();
employeeList.setVisibleRowCount(3);
}
private void buildSortPanel()
{
gridEmploySelection = new JPanel();
gridEmploySelection.setLayout(new GridLayout(10,1));
//buttons
sortListButton = new JButton("Sort List");
clearListButton = new JButton("Clear List");
gridEmploySelection.add(sortListButton);
gridEmploySelection.add(clearListButton);
//add rodio buttons
}
/*================
buildInputPanel():
is responsible for construcing
the West portion of the GUI.
Any other Jpanels used to help build
inputPanel will also be constructed
inside buildInputPanel() and then
added to inputPanel.
==================
*/
private void buildInputPanel()
{
//initializes the JPanels
gridInputPanel = new JPanel();
gridInputPanel.setLayout(new GridLayout(18,2));
[COLOR="Red"]//Combo box is initailized here
JComboBox employeeTypeBox = new JComboBox(employeeTypes);
//Add action to combo box
employeeTypeBox.addActionListener(controller);[/COLOR]
//initialize input text fields
txtName = new JTextField(BAR_SIZE);
txtSocialSecurity = new JTextField(BAR_SIZE);
txtMaritalStatus = new JTextField(BAR_SIZE);
txtSalary = new JTextField(BAR_SIZE);
txtBossName = new JTextField(BAR_SIZE);
txtDepartment = new JTextField(BAR_SIZE);
txtTitle = new JTextField(BAR_SIZE);
txtBonus = new JTextField(BAR_SIZE);
//initialize txt label fields
lblName = new JLabel("Name");
lblSocialSecurity = new JLabel("Social Security");
lblMaritalStatus = new JLabel("Marital Status");
lblSalary = new JLabel("Salary");
lblBossName = new JLabel("Boss Name");
lblDepartment = new JLabel("Department");
lblTitle = new JLabel("Title");
lblBonus = new JLabel("Bonus");
//======Add components to gridInputPanel==========
addEmployee = new JButton("Add Employee");
clearInputButton = new JButton("Clear Input");
//adding txt input
gridInputPanel.add(txtName);
gridInputPanel.add(lblName);
gridInputPanel.add(txtSocialSecurity);
gridInputPanel.add(lblSocialSecurity);
gridInputPanel.add(txtMaritalStatus);
gridInputPanel.add(lblMaritalStatus);
gridInputPanel.add(txtSalary);
gridInputPanel.add(lblSalary);
gridInputPanel.add(txtBossName);
gridInputPanel.add(lblBossName);
gridInputPanel.add(txtDepartment);
gridInputPanel.add(lblDepartment);
gridInputPanel.add(txtTitle);
gridInputPanel.add(lblTitle);
gridInputPanel.add(txtBonus);
gridInputPanel.add(lblBonus);
//Adding buttons and other stuff
gridInputPanel.add(addEmployee);
gridInputPanel.add(employeeTypeBox);
gridInputPanel.add(clearInputButton);
//adding actions to buttons
clearInputButton.addActionListener(controller);
}
/*PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP
ACCESSOR METHODS
PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP*/
public JTextField getNameTxt()
{
return txtName;
}
public JTextField getSocialSecurityTxt()
{
return txtSocialSecurity;
}
public JTextField getMaritalStatusTxt()
{
return txtMaritalStatus;
}
public JTextField getSalaryTxt()
{
return txtSalary;
}
public JTextField getBossNameTxt()
{
return txtBossName;
}
public JTextField getDepartmentTxt()
{
return txtDepartment;
}
public JTextField getTitleTxt()
{
return txtTitle;
}
public JTextField getBonusTxt()
{
return txtBonus;
}
[COLOR="Red"]//Combo box from GUI is accessed from Controller class here
public JComboBox getEmployeeTypeBox()
{
return employeeTypeBox;
}[/COLOR]
}
Thank you
Re: JComboBox null pointer Exception error
With the following code:
Code :
JComboBox employeeTypeBox = new JComboBox(employeeTypes);
You are instantiating a local variable rather than your class variable, and so your class variable remains null. If you do the following you will instantiate the class variable instead.
Code :
employeeTypeBox = new JComboBox(employeeTypes);
Re: JComboBox null pointer Exception error
Oh my gosh! Thanks! I didn't even realize that mistake even while looking for the problem. It works now. I can now finally continue with my program.
Thank you so much