1

First java

package a;
abstract class Employee{
private String staffID;
private String name;

public Employee(String staffID, String name) {
    this.staffID = staffID;
    this.name = name;
}
public String getStaffID() {
    return staffID;
}

public void setStaffID(String staffID) {
    this.staffID = staffID;
}

public String getName() {
    return name + "EM";
}

public void setName(String name) {
    this.name = name;
}
}

Second java

 package a;
 class Manager extends Employee{
 private int salary;

 public Manager(String staffID, String name, int salary) {
    super(staffID, name);
     this.salary = salary;
 }

 }

The GUI java

   package a;
   import java.io.*;
   java.awt.*;
   java.awt.event.*;
   import javax.swing.*;

   public class Gui extends JFrame implements ActionListener{
     private JLabel l1, l2;
    private JTextField i1,i2;
    private JButton b1;
    String string = "";
    public Gui(){
    super("Title");

    Container container = getContentPane();
    container.setLayout(null);

    l1 = new JLabel("Staff ID:");
    l1.setBounds(65, 31, 46, 14);
    container.add(l1);

    l2 = new JLabel("Name:");
    l2.setBounds(65, 115, 46, 14);
    container.add(l2);

    i1 = new JTextField(20);
    i1.setBounds(120,31,250,20);
    container.add(i1);      
    i2 = new JTextField(20);
    i2.setBounds(120,115,250,20);
    container.add(i2);      


    b1 = new JButton("add m");
    b1.setBounds(30,390,120,30);
    container.add(b1);
    addMButton.addActionListener(new ActionListener() { 
        public void actionPerformed(ActionEvent e) { 
            Employee emp[] = new Manager(i1.getText(),i2.getText());

            string = "Staff Id:" + Emp[].getstaffID  +"Name: "+ Emp[].getName();


            try{
                String data = "Staff.txt";
                BufferedWriter reader = new BufferedWriter(new FileWriter(data));
                reader.write(string);
                reader.newLine();
                reader.close();

            }catch (IOException E){
                System.out.println("Error is " + E);
            }
        }
    });         

    setSize(600,600enter code here);
    setVisible(true);

}   
public static void main (String args[]){
    Gui application = new Gui();
    application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}

    }
}
}

E.g the Id is A1234567 and the name is Tom Lee. How can I insert those data by the constructor?

I am a beginner and I know that there are some common mistake hopeful, someone can correct my mistake.. thanks a lot

Samsonlai
  • 11
  • 1
  • Whats the error you are getting? Also, why are you using array of Employee `Employee emp[]` ? – jarvo69 Apr 17 '17 at 15:09
  • 1
    i got Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException because i am not only insert one record, is it wrong if i using Employee emp[]? – Samsonlai Apr 17 '17 at 15:25
  • `[]` makes it an array of your `Employee` class object. Remove that if you don't want array of `Employee` class objects. I advice you to first learn basics of Java of how array works, what is class,object, how to declare variables etc. – jarvo69 Apr 18 '17 at 04:17
  • and if you want to use array, but storing and retrieving only 1 object at a time, try replacing `emp[]` with `emp[0]` everywhere after this line `Employee emp[] = new Manager(i1.getText(),i2.getText());` then see output if it works – jarvo69 Apr 18 '17 at 04:20
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – MasterBlaster Apr 18 '17 at 21:57

0 Answers0