0

我正在编写在售票处工作的程序。有两个类,乘客和预订。但是当我创建乘客类时,代码的“if”部分不起作用,并给出了 nullpointerexception 的错误。“其他”部分正在工作。这是输出和错误:

run:
Choose option for processing; 
1. Add a new passenger
2. Add a reservation for an existing Passenger 
3. Display reservations
4. Cancel reservation
0. Quit
1
enter a name :
gokhan
adress : 
nas
Number :
212
password : 
123456
Exception in thread "main" java.lang.NullPointerException
at project1.testClass.main(testClass.java:47)
Java Result: 1
BUILD SUCCESSFUL (total time: 7 seconds)   // what a succesful.

消息在此行中显示错误;

  System.out.println("password : ");
  password=input.nextLine();
    if(password.equals("ffffff")){
  System.out.println("Password should be six characters!\nYour password is: "+password);
    break ;
     }
     else { 
  passenger[count]= new Passenger(name,adress,number,password);   //  this line 
  count++;
  passenger[count].getAccID();
  System.out.println("Password should be six characters!\nYour password is: "+password);
    break ;
     }

所以这是乘客舱:

 public Passenger(String Name, String adress, String number, String password){

count++;
accID+=count;
AccountList[count]=accID;


this.Name=Name;

this.adress=adress;
this.number=number;

if(checkPw(password)==true){
this.password=password;
}
else { 
    this.password="ffffff";
}

private boolean checkPw(String password){

int length;
length = password.length();

if (length != 6) {
    return false;
    }
if (password.charAt(0)=='0'){
   return false;
}
else {
    for (int i = 0; i < password.length()-1 ;i++){
    if((password.charAt(i))==(password.charAt(i+1))){
        return false;
    }
    }
}
return true;

}
4

1 回答 1

2

你以为你初始化passenger了,但实际上你没有。这是唯一可能导致该线路出现 NPE 的事情。

于 2013-11-10T21:07:49.923 回答