Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 28th, 2012  |  syntax: None  |  size: 5.51 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Why am I getting a null pointer exception when attempting to access an object?
  2. guiEmployee1 theGuiEmployee = new guiEmployee1();
  3.        
  4. public void addInfoToSystem()
  5. {
  6.     System.out.println("about to add data to system");
  7.     Employee aEmployee = theGuiEmployee.getEmployee();
  8. }
  9.        
  10. import java.awt.*;
  11. import java.awt.event.*;
  12. import javax.swing.*;
  13. import java.util.*;
  14.  
  15. public class guiEmployee1 extends JFrame
  16. {
  17.     private static Employee theEmployee;
  18.  
  19.     private String fName;
  20.     private String sName;
  21.     private String gender;
  22.     private String pLevel;
  23.     private String empIDnumber;
  24.     private int dPayLevel, i=0;
  25.  
  26.     JTextField employeeDetails1;
  27.     JTextField employeeDetails2;
  28.     JTextField employeeDetails3;    
  29.     JTextField employeeDetails4;
  30.     JTextField employeeDetails5;
  31.  
  32.     private static ArrayList<Employee> allEmployees = new ArrayList<Employee>();
  33.  
  34.     public guiEmployee1()
  35.     {
  36.         JButton submit;
  37.         JButton b1;
  38.  
  39.         System.out.println("cabanas");
  40.  
  41.         JFrame frame = new JFrame();
  42.  
  43.         employeeDetails1 = new JTextField(10);
  44.         employeeDetails2 = new JTextField(10);
  45.         employeeDetails3 = new JTextField(10);
  46.         employeeDetails4 = new JTextField(10);
  47.         employeeDetails5 = new JTextField(10);
  48.  
  49.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  50.         frame.setSize(new Dimension(320, 75));
  51.         frame.setTitle("Employee Details");
  52.  
  53.         frame.setLayout(new FlowLayout());
  54.  
  55.         frame.add(new JLabel("Please enter Employees first Name: "));
  56.         frame.add(employeeDetails1);
  57.         ButtonListenerDepName listener = new ButtonListenerDepName();
  58.  
  59.         frame.add(new JLabel("Please enter Employees Last Name: "));
  60.         frame.add(employeeDetails2);
  61.         ButtonListenerDepName1 listener1 = new ButtonListenerDepName1();
  62.  
  63.         frame.add(new JLabel("Please enter Employees Gender: "));
  64.         frame.add(employeeDetails3);
  65.         ButtonListenerDepName2 listener2 = new ButtonListenerDepName2();
  66.  
  67.         frame.add(new JLabel("Please enter Employees Pay Level: "));
  68.         frame.add(employeeDetails4);
  69.         ButtonListenerDepName4 listener4 = new ButtonListenerDepName4();
  70.  
  71.         frame.add(new JLabel("Please enter Employees ID Number: "));
  72.         frame.add(employeeDetails5);
  73.         empIDnumber = employeeDetails5.getText();
  74.  
  75.         createNewDepartment listener5 = new createNewDepartment();
  76.  
  77.         b1 = new JButton("Submit");
  78.  
  79.         b1.addActionListener(listener5);
  80.         b1.addActionListener(listener4);
  81.  
  82.         b1.addActionListener(listener2);
  83.         b1.addActionListener(listener1);
  84.         b1.addActionListener(listener);
  85.  
  86.         frame.add(b1);
  87.         frame.pack();
  88.         frame.setSize(300,300);
  89.         frame.setVisible(true);
  90.     }
  91.  
  92.     public class ButtonListenerDepName implements ActionListener
  93.     {
  94.         public void actionPerformed (ActionEvent e )
  95.         {
  96.              fName = employeeDetails1.getText();
  97.             System.out.println("and This is the employes first name :"+ fName);        
  98.         }
  99.     }
  100.  
  101.     public class ButtonListenerDepName1 implements ActionListener
  102.     {
  103.         public void actionPerformed (ActionEvent e )
  104.         {
  105.              System.out.println("and we get to depname1");
  106.              sName = employeeDetails2.getText();
  107.             System.out.println("and This is the emp scnd name :"+ sName);  
  108.         }
  109.     }
  110.  
  111.     public class ButtonListenerDepName2 implements ActionListener
  112.     {
  113.         public void actionPerformed (ActionEvent e )
  114.         {
  115.              System.out.println("and we get to depname2");
  116.              gender = employeeDetails3.getText();
  117.             System.out.println("and This is the employes gender:"+ gender);        
  118.         }
  119.     }
  120.  
  121.     public class ButtonListenerDepName3 implements ActionListener
  122.     {
  123.         public void actionPerformed (ActionEvent e )
  124.         {
  125.              System.out.println("and we get to depname3");
  126.              empIDnumber = employeeDetails3.getText();
  127.             System.out.println("and This is the emp id: "+ empIDnumber);    
  128.         }
  129.     }
  130.  
  131.     public class ButtonListenerDepName4 implements ActionListener
  132.     {
  133.         public void actionPerformed (ActionEvent e )
  134.         {
  135.                 System.out.println("and we get to depname4");
  136.  
  137.                 try
  138.                 {
  139.                 pLevel = employeeDetails4.getText();        
  140.                 dPayLevel = Integer.parseInt(pLevel);
  141.                 }
  142.                 catch (NumberFormatException nfe)
  143.                 {
  144.                     pLevel = "-1";
  145.                     dPayLevel = Integer.parseInt(pLevel);
  146.                 }
  147.  
  148.     //      System.out.println("and This is the emp pay level :"+ dPayLevel);  
  149.         }
  150.     }
  151.  
  152.     public class createNewDepartment implements ActionListener
  153.     {
  154.         public void actionPerformed (ActionEvent e )
  155.         {
  156.             //create a new department and then adds it to thee system
  157.             theEmployee = new Employee(fName, sName, gender, dPayLevel, empIDnumber);
  158.  
  159.             storageSystem theStorageSystem = new storageSystem();
  160.             theStorageSystem.setEmployee(theEmployee);
  161.             System.out.println("mazel tov they have all been added in the gui employee");
  162.             System.out.println(theEmployee);
  163.         }
  164.     }
  165.  
  166.     public Employee getEmployee()
  167.     {
  168.         return theEmployee;
  169.     }
  170.  
  171.     public int getValue()
  172.     {
  173.          i = 1;
  174.         return i;  
  175.     }
  176.  
  177. }
  178.        
  179. public void setEmployee(Employee theEmployee)
  180.     {
  181.         this.theEmployee = theEmployee;
  182.     }
  183.        
  184. guiEmployee1 theGuiEmployee = new guiEmployee1();
  185.  
  186. theGuiEmployee.setEmployee(new Employee());