Reputation: 367
I am just trying to access a static object, containing an employee's information, from the main driver class. But when I try to do so using its getter method, I get the old employee information data from another class.
exception in thread awt event que zero java.lang null pointer exception...
I am using this line of code in main driver to instantiate a GUI to enter employee details.
guiEmployee1 theGuiEmployee = new guiEmployee1();
This is another class which allows the user to enter all employee data and then it creates a new employee object with this info.
I am using this method in the driver to access the data from the getter in the GUI I have just made previously.
public void addInfoToSystem()
{
System.out.println("about to add data to system");
Employee aEmployee = theGuiEmployee.getEmployee();
}
And this is the entire gui employee class which allows user to enter employee data and creates the new employee object I mentioned just before. It contains the getter method.
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;
}
}
Upvotes: 1
Views: 2386
Reputation: 13465
Hey u have just created a reference, an object hasn't been instantiated. First create the object then call its method, Because the method call lies in the memory in the STACK form, and it refers to the created(instantiated) object in the heap memory, unless the object is created the method will give an exception..
Upvotes: 1