2

我是 Java 新手,我试图允许用户通过 Gui 输入员工的名字和姓氏,当他们按下提交按钮时,它会激活侦听器方法并允许收集输入的值并将其放入系统内存

我的问题是,当我输入名字时,它工作得很好,但是当我输入姓氏时,它根本不起作用,我按下提交按钮,整个事情变得疯狂,错误是空指针异常“AWT 事件队列”。而且我看不出发生这种情况的原因 PLS Help

这是错误发生在行的代码

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 
4

2 回答 2

4

唯一在该行上为空并导致异常的变量是employeeDetails2,因此您必须回顾代码以查看是否以及在何处将对象分配给该变量。在执行此操作时,您将看到您在 guiEmployee1 类的构造函数中将一个对象分配给了一个employeeDetails2,但紧接在此之上,您在构造函数中重新声明了该变量。因此,分配对象的是本地employeeDetails2 变量,而不是从未初始化的类字段。这称为可变阴影。解决方法是不要在构造函数中重新声明变量。

IE:

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);

接下来,您需要重新命名变量,以便您的代码成为“自我注释”。换句话说,与其给变量取一个像employeeDetailsX 这样的通用名称,为什么不将其命名为lastNameField,将其命名为firstNameField,并将“b1”按钮命名为submitButton?这样在调试代码时,您会确切地知道它应该做什么?

于 2011-10-16T11:41:08.570 回答
3

我没有测试过,但我认为问题是:

JTextField employeeDetails2;
employeeDetails2 = new JTextField(10);

只需放弃声明,它就可以工作。

employeeDetails2 = new JTextField(10);
于 2011-10-16T11:42:45.590 回答