- Why am I getting a null pointer exception when attempting to access an object?
- guiEmployee1 theGuiEmployee = new guiEmployee1();
- public void addInfoToSystem()
- {
- System.out.println("about to add data to system");
- Employee aEmployee = theGuiEmployee.getEmployee();
- }
- 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;
- }
- }
- public void setEmployee(Employee theEmployee)
- {
- this.theEmployee = theEmployee;
- }
- guiEmployee1 theGuiEmployee = new guiEmployee1();
- theGuiEmployee.setEmployee(new Employee());