Exception in thread "main" java.lang.NullPointerException and how to fix it?

boo :

I have to make a program using Inheritance concept. I have made the base class and subclasses. But when I input the first input, it gives me Exception in thread "main" java.lang.NullPointerException. How can I fix this?

These are my codes. My main

package labweek7;

import java.awt.Menu;
import java.util.Scanner;
import java.util.Vector;

public class Main {

    Vector<String> menu =new Vector<>();
    Scanner scan = new Scanner(System.in);

    Employee emp; 
    EmployeeFullTime ft;
    EmployeePartTime pt;

    public Main() {
        int choice = 0;
        int pay;
        int time;
        int salary;


        do{
            System.out.println("ABC EMPLOYEE DATA");
            System.out.println("=================");
            System.out.println("1. Add Employee");
            System.out.println("2. View Employee");
            System.out.println("3. Resign");
            System.out.println("4. Exit");
            System.out.print("Choice: ");
            choice = scan.nextInt();
            scan.nextLine();

            switch(choice){
            case 1:
                String name = "";
                do{
                    System.out.print("Input employee name[must be more than 3 characters]: ");
                    name = scan.next();
                }while(! (name.length()>=3));
                emp.empName.add(name);

                int age;
                do{
                    System.out.print("Input employee age[>=17]: ");
                    age = scan.nextInt();
                }while(!(age>=17));
                emp.empAge.add(age);

                String role = "";
                do{
                    System.out.print("Input employee role[Assistant | Programmer](Case Sensitive): ");
                    role = scan.nextLine();
                }while(!(role.equals("Assitant") || (role.equals("Programmer"))));
                emp.empRole.add(role);

                String type = "";
                do{
                    System.out.print("Input employee type[PartTime | FullTime](Case Sensitive): ");
                    type = scan.nextLine();
                }while(!(type.equals("PartTime")|| type.equals("FullTime")));
                emp.empType.add(type);

                if(type.equals("PartTime")){
                    emp = new EmployeePartTime(name, age, role, type);
                    do{
                        System.out.print("Input pay per hour[>=10000]: ");
                        pay = scan.nextInt();
                    }while(!(pay>=10000));
                    pt.pay.add(pay);

                    do{
                        System.out.print("Input working hour per week[>0]: ");
                        time = scan.nextInt();
                    }while(!(time>0));
                    pt.hour.add(time);

                }
                else{
                    emp = new EmployeeFullTime(name, age, role, type);
                    do{
                        System.out.println("Input base salary[>=10000]: ");
                        salary = scan.nextInt();
                    }while(!(salary>=10000));
                    ft.salary.add(salary);
                }

                String status = "active";
                break;


            }
        }while(choice!=4);

    }

    void view(){
        //for(int i=1, j=0; i<)
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        new Main();
    }

}

My superclass called Employee

    package labweek7;

    import java.util.Vector;

    public abstract class Employee {

        public String name;
        public int age;
        public String role;
        public String type;

        Vector<String> empName = new Vector<>();
        Vector<Integer> empAge = new Vector<>();
        Vector<String> empRole = new Vector<>();
        Vector<String> empType = new Vector<>();

        public Employee(String name, int age, String role, String type) {
            // TODO Auto-generated constructor stub
            this.name = name;
            this.age = age;
            this.role = role;
            this.type = type;
        }

    }

My EmployeeFullTime ( sub class 1) package labweek7;

    import java.util.Vector;

    public class EmployeeFullTime extends Employee{

        Vector<Integer> salary = new Vector<>(); 

        public EmployeeFullTime(String name, int age, String role, String type) {
            super(name, age, role, type); 
        }

        public static void main(String[] args) {
            // TODO Auto-generated method stub

        }

    }

and my employeeparttime (sub class 2)

package labweek7;

import java.util.Vector;

public class EmployeePartTime extends Employee{

    Vector<Integer> pay = new Vector<>();
    Vector<Integer> hour = new Vector<>();

    public EmployeePartTime(String name, int age, String role, String type) {
        super(name, age, role, type);
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub

    }

}

When I want to input employee name, it gives me NULLpointer exception on emp.empName.add(name); line. Any help is appreciated. Thank you.

Tarun :

You are getting exception because emp is null.
You need to initialize your emp object in the main method first:

Employee emp = new EmployeeFullTime();

For the above statement to work, you should have default constructor in your EmployeeFullTime and Employee class.

Do the same for other objects ft and pt too.

 EmployeeFullTime ft = new EmployeeFullTime ();
 EmployeePartTime pt = new EmployeePartTime();

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=374480&siteId=1