gpt4 book ai didi

java - 出现空指针异常但不知道如何修复它

转载 作者:行者123 更新时间:2023-12-01 18:30:54 25 4
gpt4 key购买 nike

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

这是我的测试人员类(class):

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

}

以及空指针错误:

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)

这是我正在为其进行测试的类(class):

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

}

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

最佳答案

空指针出现在

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())' .

关于java - 出现空指针异常但不知道如何修复它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24277734/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com