Java中的票证系统,但在创建成员时主要是NullPointerException

时间:2013-11-10 20:14:53

标签: java oop nullpointerexception

我正在编写在售票处工作的程序。有两个班,乘客和预订。但是当我创建乘客类时,代码的“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;

}

1 个答案:

答案 0 :(得分:2)

你认为你初始化了passenger,但实际上你没有。这是唯一可能在该线上引起NPE的事情。