gpt4 book ai didi

java - 为什么在尝试访问对象时会出现空指针异常?

转载 作者:太空宇宙 更新时间:2023-11-04 08:29:29 24 4
gpt4 key购买 nike

我只是尝试从主驱动程序类访问包含员工信息的静态对象。但是,当我尝试使用其 getter 方法执行此操作时,我从另一个类获取了旧的员工信息数据。

exception in thread awt event que zero java.lang null pointer exception...

我在主驱动程序中使用这行代码来实例化 GUI 以输入员工详细信息。

guiEmployee1 theGuiEmployee = new guiEmployee1();

这是另一个类,它允许用户输入所有员工数据,然后使用此信息创建一个新的员工对象。

我在驱动程序中使用此方法来从我之前制作的 GUI 中的 getter 访问数据。

public void addInfoToSystem()
{
System.out.println("about to add data to system");
Employee aEmployee = theGuiEmployee.getEmployee();
}

这是整个 gui 员工类,它允许用户输入员工数据并创建我之前提到的新员工对象。它包含 getter 方法。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

public class guiEmployee1 extends JFrame
{
private static Employee theEmployee;

private String fName;
private String sName;
private String gender;
private String pLevel;
private String empIDnumber;
private int dPayLevel, i=0;

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

private static ArrayList<Employee> allEmployees = new ArrayList<Employee>();

public guiEmployee1()
{
JButton submit;
JButton b1;

System.out.println("cabanas");

JFrame frame = new JFrame();

employeeDetails1 = new JTextField(10);
employeeDetails2 = new JTextField(10);
employeeDetails3 = new JTextField(10);
employeeDetails4 = new JTextField(10);
employeeDetails5 = 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);
ButtonListenerDepName listener = new ButtonListenerDepName();

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

frame.add(new JLabel("Please enter Employees Gender: "));
frame.add(employeeDetails3);
ButtonListenerDepName2 listener2 = new ButtonListenerDepName2();

frame.add(new JLabel("Please enter Employees Pay Level: "));
frame.add(employeeDetails4);
ButtonListenerDepName4 listener4 = new ButtonListenerDepName4();

frame.add(new JLabel("Please enter Employees ID Number: "));
frame.add(employeeDetails5);
empIDnumber = employeeDetails5.getText();

createNewDepartment listener5 = new createNewDepartment();

b1 = new JButton("Submit");

b1.addActionListener(listener5);
b1.addActionListener(listener4);

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

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

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

public class ButtonListenerDepName1 implements ActionListener
{
public void actionPerformed (ActionEvent e )
{
System.out.println("and we get to depname1");
sName = employeeDetails2.getText();
System.out.println("and This is the emp scnd name :"+ sName);
}
}

public class ButtonListenerDepName2 implements ActionListener
{
public void actionPerformed (ActionEvent e )
{
System.out.println("and we get to depname2");
gender = employeeDetails3.getText();
System.out.println("and This is the employes gender:"+ gender);
}
}

public class ButtonListenerDepName3 implements ActionListener
{
public void actionPerformed (ActionEvent e )
{
System.out.println("and we get to depname3");
empIDnumber = employeeDetails3.getText();
System.out.println("and This is the emp id: "+ empIDnumber);
}
}

public class ButtonListenerDepName4 implements ActionListener
{
public void actionPerformed (ActionEvent e )
{
System.out.println("and we get to depname4");

try
{
pLevel = employeeDetails4.getText();
dPayLevel = Integer.parseInt(pLevel);
}
catch (NumberFormatException nfe)
{
pLevel = "-1";
dPayLevel = Integer.parseInt(pLevel);
}

// System.out.println("and This is the emp pay level :"+ dPayLevel);
}
}

public class createNewDepartment implements ActionListener
{
public void actionPerformed (ActionEvent e )
{
//create a new department and then adds it to thee system
theEmployee = new Employee(fName, sName, gender, dPayLevel, empIDnumber);

storageSystem theStorageSystem = new storageSystem();
theStorageSystem.setEmployee(theEmployee);
System.out.println("mazel tov they have all been added in the gui employee");
System.out.println(theEmployee);
}
}

public Employee getEmployee()
{
return theEmployee;
}

public int getValue()
{
i = 1;
return i;
}

}

最佳答案

嘿,你刚刚创建了一个引用,对象尚未实例化。首先创建对象然后调用它的方法,因为方法调用是以STACK形式存在于内存中的,并且引用的是堆内存中创建(实例化)的对象,除非该对象被创建了该方法才会抛出异常。

关于java - 为什么在尝试访问对象时会出现空指针异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7805777/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com