我是 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