Employee database using Employee Object Array but getting NullPointerException

4214 views java
2

I am not understanding where to edit in my code. I am trying to create an employee database using Employee object array. It's giving java.lang.NullPointerException at Line24. Please help me understand.

package multipleEmployeeData;

import java.util.Scanner;

public class MultipleEmployeeData extends EmployeeDetail{
    static EmployeeDetail[] emp=null;


    public static void empDataoutput() {
        System.out.println("Employee Database: ");
        System.out.println("EmployeeName"+"\t"+"EmployeeNumber"+"\t"+"EmployeeSalary"+"\t"+"EmployeeMobileNumber");
        for(int i=0;i<3;i++ ) {
            System.out.print(emp[i].EmpName+"\t"+emp[i].EmpNumber+"\t"+emp[i].Salary+"\t"+emp[i].MobNumber);
            System.out.println();
        }
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner scn=new Scanner(System.in);
        emp=new EmployeeDetail[3];
        for(int i=0;i<3;i++) {
            System.out.println("Enter the employee "+(i+1)+" name");
            emp[i].EmpName=scn.nextLine();
            System.out.println("Enter the employee "+(i+1)+" number");
            emp[i].EmpNumber=Integer.parseInt(scn.nextLine());
            System.out.println("Enter the employee "+(i+1)+" Mobile number");
            emp[i].MobNumber=Long.parseLong(scn.nextLine());
            System.out.println("Enter the employee "+(i+1)+" Salary");
            emp[i].Salary=Float.parseFloat(scn.nextLine());
        }
        scn.close();

        empDataoutput();    
    }

}

class EmployeeDetail{
    String EmpName;
    int EmpNumber;
    Float Salary;
    Long MobNumber;
}

answered question

which line of code is giving NPE?

It is because emp[i] is null which is causing the NPE

But i am taking user input there. How to resolve this?

2 Answers

8

change the code like below:

public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner scn=new Scanner(System.in);
        emp=new EmployeeDetail[3];
        for(int i=0;i<3;i++) {
            emp[i] = new EmployeeDetail();
            System.out.println("Enter the employee "+(i+1)+" name");
            emp[i].EmpName=scn.nextLine();
            System.out.println("Enter the employee "+(i+1)+" number");
            emp[i].EmpNumber=Integer.parseInt(scn.nextLine());
            System.out.println("Enter the employee "+(i+1)+" Mobile number");
            emp[i].MobNumber=Long.parseLong(scn.nextLine());
            System.out.println("Enter the employee "+(i+1)+" Salary");
            emp[i].Salary=Float.parseFloat(scn.nextLine());
        }
        scn.close();

        empDataoutput();    
    }

Look at emp[i] = new EmployeeDetail(); where object is instantiated and added to index of emp array.

posted this
9

You need to instantiate the EmployeeDetail object in the loop:

   for(int i=0;i<3;i++) {
        emp[i] = new EmployeeDetail();
        System.out.println("Enter the employee "+(i+1)+" name");
        emp[i].EmpName=scn.nextLine();
        System.out.println("Enter the employee "+(i+1)+" number");
        emp[i].EmpNumber=Integer.parseInt(scn.nextLine());
        System.out.println("Enter the employee "+(i+1)+" Mobile number");
        emp[i].MobNumber=Long.parseLong(scn.nextLine());
        System.out.println("Enter the employee "+(i+1)+" Salary");
        emp[i].Salary=Float.parseFloat(scn.nextLine());
    }

posted this

Have an answer?

JD

Please login first before posting an answer.