Why would I be getting a Null pointer exception with Gui input and listener methods?

℡╲_俬逩灬. 提交于 2019-12-24 05:22:05

问题


I am new to Java and I am trying to allow a user to enter an employees first and last name via the Gui and when they press the submit button it activates the listener methods and allows the values entered to be gathered and put in the systems memory

My issue is that when I enter the first name it works perfectly but when I enter the last name it does not work at all I press the submit button and the the whole thing goes nuts the error is null pointer exception "AWT event queue". And I can see no reason for this happening PLS Help

This is the code the error occurs at line

lName = employeeDetails2.getText();  (located closer to the end of the code)
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

public class guiEmployee1 extends JFrame
{

    private String  fName;
    private String  lName;
    private String  gender;
    private String  payLevel;
    private String  empIDnumber;
//  private int dPayLevel; 

    JTextField employeeDetails1;
    JTextField employeeDetails2;
    JTextField employeeDetails3;    
    JTextField employeeDetails4;
    JTextField employeeDetails5;

    public guiEmployee1()
    {
        JButton submit;
        JButton b1;

        System.out.println("cabanas");

        JFrame frame = new JFrame();
        employeeDetails1 = new JTextField(10);

        JTextField employeeDetails2;
        employeeDetails2 = new JTextField(10);




        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(new Dimension(320, 75));
        frame.setTitle("Employee Details");

        frame.setLayout(new FlowLayout());

        frame.add(new JLabel("Please enter Employees first Name: "));
        frame.add(employeeDetails1);
        ButtonListenerFirstName listener = new ButtonListenerFirstName();

        frame.add(new JLabel("Please enter Employees Last Name: "));
        frame.add(employeeDetails2);
        ButtonListenerLastName listener1 = new  ButtonListenerLastName();




        b1  = new JButton  ("Submit");

        b1.addActionListener(listener);
        b1.addActionListener(listener1);


        frame.add(b1);
        frame.pack();
        frame.setSize(300,300);
        frame.setVisible(true);



    }


     public class ButtonListenerFirstName implements ActionListener
    {
        public void actionPerformed (ActionEvent e )
        {
             fName = employeeDetails1.getText();
            System.out.println("and This is the employes first name :"+ fName);         
        } 
    }

    public class ButtonListenerLastName implements ActionListener
    {
        public void actionPerformed (ActionEvent e )
        {
             lName = employeeDetails2.getText();
            System.out.println("and This is the employes Last name :"+ lName);      
        } 
    }




    public static Department getDepartment()
    {
        return null;
    }


}


Thanks 

回答1:


The only variable which an be null on that line and cause that exception is the employeeDetails2, so you must look back in your code to see if and where you assign an object to that variable. On doing this, you'll see that you assign an object to a employeeDetails2 in the guiEmployee1 class's constructor, but immediately above this you re-declare the variable in the constructor. Thus it is the local employeeDetails2 variable which has been assigned an object, not the class field which is never initialized. This is called variable shadowing. The solution is not to redeclare the variable in the constructor.

i.e.:

public guiEmployee1()
{
    JButton submit;
    JButton b1;

    System.out.println("cabanas");

    JFrame frame = new JFrame();
    employeeDetails1 = new JTextField(10);

    //  JTextField employeeDetails2; ***** comment out this line *****
    employeeDetails2 = new JTextField(10);

Next, you'll want to re-name your variables so that your code becomes "self-commenting". In other words, rather than giving the variable such a general name as employeeDetailsX, why not instead call it lastNameField, and the one before it firstNameField, and the "b1" button to submitButton? That way when debugging your code, you'll know exactly what it's supposed to be doing?




回答2:


i have not tested it, but i think the problem is:

JTextField employeeDetails2;
employeeDetails2 = new JTextField(10);

just leave away the declaration and it sould work.

employeeDetails2 = new JTextField(10);


来源:https://stackoverflow.com/questions/7783998/why-would-i-be-getting-a-null-pointer-exception-with-gui-input-and-listener-meth

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!