public class Employee {


    private String Name;
    private String SIN;
    private Integer Salary;

    public Employee(String string, String string2, int i) {
        // TODO Auto-generated constructor stub
    }
    public Employee()

    {

    }
    public Employee(String N, String sin, Integer Sal)
    {
        Name = N;
        SIN = sin;
        Salary = Sal;
    }

    public String getname()
    {
        return Name;
    }

    public String getsin()
    {
        return SIN;
    }

    public Integer getsalary()
    {
        return Salary;
    }

    public void setname(String N)
    {
        Name = N;
    }

    public  void setsin(String sin)
    {
        SIN = sin;
    }

    public void setsalary(Integer sal)
    {
        Salary = sal;
    }


    public void display ()
    {
        System.out.println("Name: " + getname() + "\nSIN: " + getsin() + "\nSalary: "+ getsalary());
    }
}




import javax.swing.JOptionPane;
public class EmployeeTest {

    /**
     * @param args
     */
    public static void main(String[] args) {


        Employee a = new Employee("Ferz", "564328946", 8000);
        Employee e = new Employee();
        e.display();


        JOptionPane.showMessageDialog(null, "Name: " + a.getname() + "\nSIN: " + a.getsin()+
                "\nSalary: " + a.getsalary(), "Employee Info", JOptionPane.PLAIN_MESSAGE ) ;


    }

}

the above is my code.....

when am running the program am getting the null in both employee object... help me

Edited 3 Years Ago by Dani: Formatting fixed

You get null because all the properties of your class are not initialized.

You haven't defined the first constructor (with "int" as third argument).

If you call with and integer value, method overloading will select the method with "int" as parameter over "Integer". Hence when first object is created the first constructor will be called. and you are not doing anything in that constructor. So define it.

And also coming to the second one. You are not passing any values when creating the second object, then how will the members be initialized?? use the setter methods to set the data for the members of second object.

Hope u understand.....

yes it works.... this is my first java program.... thanks for your help... I just changed the constructor's position...

Employee e = new Employee();
Employee a = new Employee("Ferz", "564328946", 8000);

You changed its position? Cud u post the code? And mark the thread as solved if youve cleared all your doubts.

public static void main(String[] args) {

        Employee e = new Employee();
        Employee a = new Employee("Farz", "564328946", 8000);
        Employee K = new Employee();

        e.display();


        JOptionPane.showMessageDialog(null, "Name: " + a.getname() + "\nSIN: " + a.getsin()+
                "\nSalary: " + a.getsalary(), "Employee Info", JOptionPane.PLAIN_MESSAGE ) ;

        String data = JOptionPane.showInputDialog ("Enter Name for Employee K: ") ;
        K.setname(data);

        String data1 = JOptionPane.showInputDialog ("Enter SIN for Employee K: ") ;
        K.setsin(data1);

        JOptionPane.showInputDialog ("Enter Name for Employee K: ") ;
        Integer data2 = sc.nextInt();
        K.setsalary(data2);


        K.display();
        }
}

Edited 3 Years Ago by Dani: Formatting fixed

This question has already been answered. Start a new discussion instead.