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

时间:2011-10-18 10:25:13

标签: java object nullpointerexception accessor

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

  线程awt事件中的

异常为零java.lang空指针异常...

我在主驱动程序中使用这行代码来实例化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;   
    }

}

1 个答案:

答案 0 :(得分:1)

嘿,你刚刚创建了一个引用,一个对象尚未实例化。首先创建对象然后调用它的方法, 因为方法调用位于STACK形式的内存中,并且它引用堆内存中创建的(实例化的)对象,除非创建该对象,否则该方法将给出异常..