スレッドの例外「メイン」java.lang.NullPointerExceptionが、どのようにそれを修正するには?

BOO:

私は、継承の概念を使用してプログラムを作成する必要があります。私は、基本クラスとサブクラスを作ってきました。しかし、私の入力最初の入力は、それは私のスレッド「メイン」のjava.lang.NullPointerExceptionの例外を与えたとき。私はこれをどのように修正することができますか?

これらは私のコードです。私の主な

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();
    }

}

従業員と呼ばれる私のスーパークラス

    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;
        }

    }

マイEmployeeFullTime(サブクラス1)パッケージ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

        }

    }

そして私のemployeeparttime(サブクラス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

    }

}

私は入力従業員の名前にしたい場合は、それは私emp.empName.add(名)上にNullPointer例外を提供します。ライン。すべてのヘルプは大歓迎です。ありがとうございました。

タルン:

EMPがnullであるので、あなたは、例外を取得しています。
あなたは、最初のメインの方法であなたのempオブジェクトを初期化する必要があります。

Employee emp = new EmployeeFullTime();

仕事に上記の文では、あなたのEmployeeFullTimeとEmployeeクラスのデフォルトコンストラクタを持つ必要があります。

あまりにも他のオブジェクトのフィートとPTのために同じことをします。

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

おすすめ

転載: http://10.200.1.11:23101/article/api/json?id=374969&siteId=1