为什么在Gui输入和侦听器方法中会出现Null指针异常? [英] Why would I be getting a Null pointer exception with Gui input and listener methods?

查看:104
本文介绍了为什么在Gui输入和侦听器方法中会出现Null指针异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我的问题是,当我输入姓氏时,它可以正常工作,但是当我输入姓氏时,它根本不起作用,我按了提交按钮,整个事情变糟了,错误是空指针异常"AWT事件队列".而且我看不出发生这种情况的原因PLS帮助

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 

推荐答案

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

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.

即:

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,并将其命名为SubmitButton的"b1"按钮?这样,在调试代码时,您会确切知道它应该做什么?

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?

这篇关于为什么在Gui输入和侦听器方法中会出现Null指针异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆