为什么我会在使用 Gui 输入和侦听器方法时遇到空指针异常?

发布于 2024-12-10 14:16:00 字数 2581 浏览 0 评论 0原文

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

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

这是行中发生错误的代码

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 

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 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倾听心声的旋律 2024-12-17 14:16:00

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

即:

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?这样在调试代码时,您就会确切地知道它应该做什么?

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?

从此见与不见 2024-12-17 14:16:00

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

JTextField employeeDetails2;
employeeDetails2 = new JTextField(10);

只要去掉声明,它就可以工作。

employeeDetails2 = new JTextField(10);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文