获取空指针异常并且不确定如何修复它

时间:2014-06-18 05:35:57

标签: java arrays nullpointerexception

我正在为一个简单的输入数据库程序创建一个测试器类。我不需要存储信息也不需要删除任何内容,但是我的类中有两个数组,我正在运行我的测试器类。

这是我的测试人员课程:

import java.util.Scanner;

public class Asst_3 {

    private static Scanner keyboard;

    public Asst_3(){

        this.keyboard = new Scanner(System.in);
    }

    public static void main(String[]args){

        Policy insurance = new Policy();

        insurance.setCustomerLast(null);
        insurance.setCustomerFirst(null);
        insurance.setPolicyNumber();
        insurance.setAge();
        insurance.setAccidentNumber();
        insurance.setPremiumDueDate(00,00,0000);
        //insurance.toString();

        System.out.println("Welcome to Drive-Rite Insurance Company");
        showMenuOptions();

        Scanner keyboard = new Scanner(System.in);
        int choice = keyboard.nextInt();
        intiateMenuSelection(choice);
    }

    private static void intiateMenuSelection(int selectedOption) {
        switch (selectedOption){
        case 1: newPolicy(null);
            break;
        case 2: returnFromAge();
            break;
        case 3: returnFromDue();
            break;
        case 4: System.out.println("Goodbye");
                System.exit(0);
            break;
        default: break;
        }
    }

    private static void newPolicy(Policy insurance) {
        System.out.println("Enter Customer's Policy Number: ");
        int poliNum = keyboard.nextInt();
        insurance.getPolicyNumber();
        System.out.println("Customer's Policy Number is: " + keyboard.nextInt());

        System.out.println("Enter Customer's Last Name: ");
        String custLast = keyboard.nextLine();
        insurance.getCustomerLast();
        System.out.println("Customer's Last Name is: " + keyboard.nextInt());

        System.out.println("Enter Customer's First Name: ");
        String custFirst = keyboard.nextLine();
        insurance.getCustomerFirst();
        System.out.println("Customer's First Name is: " + keyboard.nextInt());

        System.out.println("Enter Customer's Age: ");
        int custAge = keyboard.nextInt();
        insurance.getAge();
        System.out.println("Customer's Age is: " + keyboard.nextInt());

        System.out.println("Enter Customer's Amount of Previous Accident Reaports in Past 3 years: ");
        int custAccident = keyboard.nextInt();
        insurance.getAccidentNumber();
        System.out.println("Customer's Amount of Accidents is: " + keyboard.nextInt());

        System.out.println("Enter Customer's next Premium Due Date: ");
        int dueDate = keyboard.nextInt();
        insurance.getPremiumDueDate();
        System.out.println("Customer's Next Due Date is: " + keyboard.nextInt());

        insurance.toString();

        showMenuOptions();
    }

    private static void returnFromDue() {

        showMenuOptions();
    }

    private static void returnFromAge() {

        showMenuOptions();
    }

    private static void returnToMenu() {

        intiateMenuSelection(0);
    }

    private static void showMenuOptions() {

        System.out.println("Choose a menu option: ");
        System.out.println("(1) Create New Policies");
        System.out.println("(2) Search by age");
        System.out.println("(3) Search by due date");
        System.out.println("(4) Exit");
        System.out.print("Input Option Number ---> ");
    }

}

Null指针错误:

Exception in thread "main" java.lang.NullPointerException
  at Asst_3.newPolicy(Asst_3.java:55)
  at Asst_3.intiateMenuSelection(Asst_3.java:40)
  at Asst_3.main(Asst_3.java:35)

这是我正在制作测试人员的课程:

import java.util.*;

public class Policy {

    private int policyNumber;
    private int age;
    private int accidentNumber;
    private String customerLast;
    private String customerFirst;
    private int [] months;
    private int [] premiumDueDate;

    public Policy() {

        this.policyNumber = 0;
        this.age = 0;
        this.accidentNumber = 0;
        this.customerLast = "";
        this.customerFirst = "";
        this.premiumDueDate = new int [3];
        this.premiumDueDate[0] = 0;
        this.premiumDueDate[1] = 0;
        this.premiumDueDate[2] = 0;
        this.months = new int [12];
        this.months[0] = this.months[2] = this.months[4] = this.months[6] = this.months[7] = this.months[9] = this.months[11] = 31;
        this.months[1] = 28;
        this.months[3] = this.months[5] = this.months[8] = this.months[10] = 30;
    }

    public int getPolicyNumber(){
        return this.policyNumber;
    }

    public void setPolicyNumber(){
        if(policyNumber < 1000){
            policyNumber = 0;
        }
        if(policyNumber > 9999){
            policyNumber = 0;
        }
    }

    public int[] getPremiumDueDate(){
        return this.premiumDueDate;
    }

    public void setPremiumDueDate(int month, int day, int year){
        this.premiumDueDate[0] = month;
        this.premiumDueDate[1] = day;
        this.premiumDueDate[2] = year;

        if(month < 0||month >= 12)
        {
            this.premiumDueDate[0] = 0;
            this.premiumDueDate[1] = 0;
            this.premiumDueDate[2] = 0;
        }
        else if(day < 0 || day > this.months[month])
        {
            this.premiumDueDate[0] = 0;
            this.premiumDueDate[1] = 0;
            this.premiumDueDate[2] = 0;
        }
    }

    public int getAge(){
        return this.age;
    }

    public void setAge(){
        this.age = 0;
    }

    public int getAccidentNumber(){
        return this.accidentNumber;
    }

    public void setAccidentNumber(){
        this.accidentNumber = 0;
    }

    public String getCustomerLast(){
        return this.customerLast;
    }

    public void setCustomerLast(String customerLast){
        this.customerLast = customerLast;
    }

    public String getCustomerFirst(){
        return this.customerFirst;
    }

    public void setCustomerFirst(String customerFirst){
        this.customerFirst = customerFirst;
    }

    public String toString(){
        return "\n Policy Number: " + this.policyNumber + "\n Customer Last Name: " + this.customerLast + "\n Customer First Name: " + this.customerFirst 
            + "\n Customer age: " + this.age + "\n Number of Accidents in Past Three Years: " + this.accidentNumber + "\n Premium Due Date: " + this.premiumDueDate;
    }

}

谢谢大家,太棒了!

以下是我编辑过的代码:

测试员

import java.util.Scanner;

public class Asst_3 {

private static Scanner keyboard;

public Asst_3(){

    this.keyboard = new Scanner(System.in);
}

public static void main(String[]args){

    System.out.println("Welcome to Drive-Rite Insurance Company");
    showMenuOptions();

    int choice = keyboard.nextInt();
    intiateMenuSelection(choice);
}

private static void intiateMenuSelection(int selectedOption) {
    switch (selectedOption){
    case 1: newPolicy(new Policy());
        break;
    case 2: returnFromAge();
        break;
    case 3: returnFromDue();
        break;
    case 4: System.out.println("Goodbye");
            System.exit(0);
        break;
    default: break;
    }
}

private static void newPolicy(Policy insurance) {
    System.out.println("Enter Customer's Policy Number: ");
    int poliNum = keyboard.nextInt();
    insurance.setPolicyNumber(poliNum);
    System.out.println("Customer's Policy Number is: " + insurance.getPolicyNumber());

    System.out.println("Enter Customer's Last Name: ");
    String custLast = keyboard.nextLine();
    insurance.setCustomerLast(custLast);
    System.out.println("Customer's Last Name is: " + insurance.getCustomerLast());

    System.out.println("Enter Customer's First Name: ");
    String custFirst = keyboard.nextLine();
    insurance.setCustomerFirst(custFirst);
    System.out.println("Customer's First Name is: " + insurance.getCustomerFirst());

    System.out.println("Enter Customer's Age: ");
    int custAge = keyboard.nextInt();
    insurance.setAge(custAge);
    System.out.println("Customer's Age is: " + insurance.getAge());

    System.out.println("Enter Customer's Amount of Previous Accident Reaports in Past 3 years: ");
    int custAccident = keyboard.nextInt();
    insurance.setAccidentNumber(custAccident);
    System.out.println("Customer's Amount of Accidents is: " + insurance.getAccidentNumber());

    System.out.println("Enter Customer's next Premium Due Date: ");
    int dueDate = keyboard.nextInt();
    insurance.setPremiumDueDate(dueDate, dueDate, dueDate);
    System.out.println("Customer's Next Due Date is: " + insurance.getPremiumDueDate());

    insurance.toString();

    returnToMenu();
}

private static void returnFromDue() {

    showMenuOptions();
}

private static void returnFromAge() {

    showMenuOptions();
}

private static void returnToMenu() {

    intiateMenuSelection(0);
}

private static void showMenuOptions() {

    System.out.println("Choose a menu option: ");
    System.out.println("(1) Create New Policies");
    System.out.println("(2) Search by age");
    System.out.println("(3) Search by due date");
    System.out.println("(4) Exit");
    System.out.print("Input Option Number ---> ");
}

}

正在测试的课程:

import java.util.*;

public class Policy {

private int policyNumber;
private int age;
private int accidentNumber;
private String customerLast;
private String customerFirst;
private int [] months;
private int [] premiumDueDate;

public Policy() {

    this.policyNumber = 0;
    this.age = 0;
    this.accidentNumber = 0;
    this.customerLast = "";
    this.customerFirst = "";
    this.premiumDueDate = new int [3];
    this.premiumDueDate[0] = 0;
    this.premiumDueDate[1] = 0;
    this.premiumDueDate[2] = 0;
    this.months = new int [12];
    this.months[0] = this.months[2] = this.months[4] = this.months[6] = this.months[7] = this.months[9] = this.months[11] = 31;
    this.months[1] = 28;
    this.months[3] = this.months[5] = this.months[8] = this.months[10] = 30;
}

public int getPolicyNumber(){
    return this.policyNumber;
}

public void setPolicyNumber(int policyNumber){
    if(policyNumber < 1000){
        policyNumber = 0;
    }
    if(policyNumber > 9999){
        policyNumber = 0;
    }
}

public int[] getPremiumDueDate(){
    return this.premiumDueDate;
}

public void setPremiumDueDate(int month, int day, int year){
    this.premiumDueDate[0] = month;
    this.premiumDueDate[1] = day;
    this.premiumDueDate[2] = year;

    if(month < 0||month >= 12)
    {
        this.premiumDueDate[0] = 0;
        this.premiumDueDate[1] = 0;
        this.premiumDueDate[2] = 0;
    }
    else if(day < 0 || day > this.months[month])
    {
        this.premiumDueDate[0] = 0;
        this.premiumDueDate[1] = 0;
        this.premiumDueDate[2] = 0;
    }
}

public int getAge(){
    return this.age;
}

public void setAge(int age){
    this.age = 0;
}

public int getAccidentNumber(){
    return this.accidentNumber;
}

public void setAccidentNumber(int accidentNumber){
    this.accidentNumber = 0;
}

public String getCustomerLast(){
    return this.customerLast;
}

public void setCustomerLast(String customerLast){
    this.customerLast = customerLast;
}

public String getCustomerFirst(){
    return this.customerFirst;
}

public void setCustomerFirst(String customerFirst){
    this.customerFirst = customerFirst;
}

public String toString(){
    return "\n Policy Number: " + this.policyNumber + "\n Customer Last Name: " + this.customerLast + "\n Customer First Name: " + this.customerFirst 
        + "\n Customer age: " + this.age + "\n Number of Accidents in Past Three Years: " + this.accidentNumber + "\n Premium Due Date: " + this.premiumDueDate;
}

}

但是现在执行程序后出现了一个新错误:

Welcome to Drive-Rite Insurance Company
Choose a menu option: 
(1) Create New Policies
(2) Search by age
(3) Search by due date
(4) Exit
Input Option Number ---> Exception in thread "main" java.lang.NullPointerException
    at Asst_3.main(Asst_3.java:23)

这是NPE已修复的更新版本:

    import java.util.Scanner;

    public class Asst_3 {

private static Scanner keyboard;

public static void main(String[]args){

    System.out.println("Welcome to Drive-Rite Insurance Company");
    showMenuOptions();


    this.keyboard = new Scanner(System.in);

    int choice = keyboard.nextInt();
    intiateMenuSelection(choice);
}

private static void intiateMenuSelection(int selectedOption) {
    switch (selectedOption){

    case 1: newPolicy(new Policy());
        break;

    case 2: returnFromAge();
        break;

    case 3: returnFromDue();
        break;

    case 4: System.out.println("Goodbye");
            System.exit(0);
        break;

    default: break;
    }
}

private static void newPolicy(Policy insurance) {
    System.out.println("Enter Customer's Policy Number: ");
    int poliNum = keyboard.nextInt();
    insurance.setPolicyNumber(poliNum);
    System.out.println("Customer's Policy Number is: " + insurance.getPolicyNumber());

    System.out.println("Enter Customer's Last Name: ");
    String custLast = keyboard.nextLine();
    insurance.setCustomerLast(custLast);
    System.out.println("Customer's Last Name is: " + insurance.getCustomerLast());

    System.out.println("Enter Customer's First Name: ");
    String custFirst = keyboard.nextLine();
    insurance.setCustomerFirst(custFirst);
    System.out.println("Customer's First Name is: " + insurance.getCustomerFirst());

    System.out.println("Enter Customer's Age: ");
    int custAge = keyboard.nextInt();
    insurance.setAge(custAge);
    System.out.println("Customer's Age is: " + insurance.getAge());

    System.out.println("Enter Customer's Amount of Previous Accident Reaports in Past 3 years: ");
    int custAccident = keyboard.nextInt();
    insurance.setAccidentNumber(custAccident);
    System.out.println("Customer's Amount of Accidents is: " + insurance.getAccidentNumber());

    System.out.println("Enter Customer's next Premium Due Date: ");
    int dueDate = keyboard.nextInt();
    insurance.setPremiumDueDate(dueDate, dueDate, dueDate);
    System.out.println("Customer's Next Due Date is: " + insurance.getPremiumDueDate());

    insurance.toString();

    returnToMenu();
}

private static void returnFromDue() {

    showMenuOptions();
}

private static void returnFromAge() {

    showMenuOptions();
}

private static void returnToMenu() {

    intiateMenuSelection(0);
}

private static void showMenuOptions() {

    System.out.println("Choose a menu option: ");
    System.out.println("----------------------------------------");
    System.out.println("(1) Create New Policies");
    System.out.println("(2) Search by age");
    System.out.println("(3) Search by due date");
    System.out.println("(4) Exit");
    System.out.print("Input Option Number ---> ");
}

}

错误是我的键盘变量有问题。

4 个答案:

答案 0 :(得分:2)

中出现空指针
private static void newPolicy(Policy insurance) 

这种方法。对于这些行

insurance.getPolicyNumber();

insurance.get....();

insurance.get....();

因为引用/对象insurance作为参数从其被调用的位置出现。在你的情况下,你将null传递给方法

newPolicy(null); //try to pass a reference of Policy  like  'newPolicy(new Policy())' .

答案 1 :(得分:1)

您将变量视为阴影,即您已将keyboard声明为类变量

public class Asst_3 {

    private static Scanner keyboard;

但在main中,您已将其重新声明为本地变量...

Scanner keyboard = new Scanner(System.in);

这意味着当您致电null时,类变量仍为newPolicy

首先删除重新声明......

//Scanner keyboard = new Scanner(System.in);
keyboard = new Scanner(System.in);

这会引导你在NullPointerException

newPolicy下一次打击你
insurance.getPolicyNumber();

由于您调用方法传递null值...

newPolicy(null);

我会让你解决这个问题;)

提示: newPolicy应该不带参数,并且应该返回Policy的新实例,然后可以通过其他方法进行操作;)

答案 2 :(得分:1)

你两次声明keybord。

从此行中删除Scanner

Scanner keyboard = new Scanner(System.in);

所以你有:

keyboard = new Scanner(System.in);

答案 3 :(得分:0)

您传递给insurance的{​​{1}}为空

newPolicy

因此

case 1: newPolicy(null);

将抛出NPE