Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: JComboBox null pointer Exception error

  1. #1
    Junior Member
    Join Date
    Nov 2009
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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.



    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.......................

    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


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: JComboBox null pointer Exception error

    With the following 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.
    employeeTypeBox = new JComboBox(employeeTypes);

  3. The Following User Says Thank You to copeg For This Useful Post:

    F_Arches (November 29th, 2009)

  4. #3
    Junior Member
    Join Date
    Nov 2009
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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

Similar Threads

  1. Need Help with my JComboBox and Textfield
    By superawesome in forum AWT / Java Swing
    Replies: 1
    Last Post: November 10th, 2009, 12:15 PM
  2. Null Pointer Exception
    By MysticDeath in forum Exceptions
    Replies: 2
    Last Post: October 24th, 2009, 01:49 PM
  3. JComboBox doubt
    By rajaramesh.tadi in forum AWT / Java Swing
    Replies: 0
    Last Post: August 24th, 2009, 08:18 AM
  4. Getting Null Pointer Exception in runtime
    By RoadRunner in forum Exceptions
    Replies: 1
    Last Post: April 26th, 2009, 01:21 PM