我得到了一个NullPointerException
:
If(bookingList.size() == 0)
,
bookingList.add(vehicleBooking)
和
bookingList.add(rvBooking)
而且我不确定是什么原因造成的。任何帮助深表感谢。
堆栈跟踪
bookingSystem.FerryBookingSystem at localhost:59034
Thread [main] (Suspended (exception NullPointerException))
FerryBookingSystem.bookingIDExists(String) line: 35
FerryBookingSystem.addVehicleBooking() line: 49
FerryBookingSystem.main(String[]) line: 114
C:\Program Files\Java\jre7\bin\javaw.exe (14/05/2013 10:52:02 PM)
预订异常
package bookingSystem;
import java.util.ArrayList;
import java.util.Scanner;
import java.io.*;
class BookingException extends Exception{
String message;
String IDs;
public BookingException(String message){
this.message = message;
}
public BookingException(String message, String IDs){
this.message = message;
this.IDs = IDs;
}
public String getIDs(){
return IDs;
}
public String getMessage(){
return message;
}
}
轮渡预订系统
public class FerryBookingSystem {
private static ArrayList<VehicleBooking> bookingList;
private static Scanner userInput = new Scanner (System.in);
public FerryBookingSystem(){
bookingList = new ArrayList<VehicleBooking>();
}
public static int bookingIDExists(String IDs){
if (bookingList.size() == 0)
return -1;
for (int i = 0; i < bookingList.size(); i++){
if(bookingList.get(i).getbookingID().equals(IDs))
return i;
}
return -1;
}
public static boolean addVehicleBooking(){
System.out.print("Please enter the booking ID: ");
String booking_ID = userInput.nextLine();
if(bookingIDExists(booking_ID) != 1)
{
System.out.println("\nError - Sale ID \"" + booking_ID +
"\" already exists in the system!");
return false;
}
System.out.print("Please enter the registration number for the vehicle: ");
String registration_Number = userInput.nextLine();
System.out.print("Please enter the vehicle " +
"description: ");
String vehicle_Description = userInput.nextLine();
System.out.print("Please enter the number of people travelling in the vehicle: ");
int travelling_Number = userInput.nextInt();
userInput.nextLine();
VehicleBooking vehicleBooking = new VehicleBooking(booking_ID, registration_Number, vehicle_Description, travelling_Number);
bookingList.add(vehicleBooking);
System.out.println("New vehicle booking added successfully for " + booking_ID);
return true;
}
public static boolean addRecreationalVehicleBooking(){
System.out.print("Please enter the booking ID: ");
String booking_ID = userInput.nextLine();
System.out.print("Please enter the registration number for the vehicle: ");
String registration_Number = userInput.nextLine();
System.out.print("Please enter the vehicl description: ");
String vehicle_Description = userInput.nextLine();
System.out.print("Please enter the number of people travelling in the vehicle: ");
int travelling_Number = userInput.nextInt();
userInput.nextLine();
RVBooking rvBooking = new RVBooking(booking_ID, registration_Number, vehicle_Description, travelling_Number);
bookingList.add(rvBooking);
System.out.println("New rvbooking added successfully for " + booking_ID);
return true;
}
public static void displayBookingSummary(){
if (bookingList.size() != 0){
System.out.println("\nSummary of all past vehicle booking stored on system.");
for (int i=0 ; i<bookingList.size() ; i++){
bookingList.get(i).printBookingSummary();
System.out.println("");
}
}
}
public static void main (String [] args) throws IOException{
char user;
do{
System.out.println("**** Ferry Ticketing System ****");
System.out.println(" A - Add Vehicle Booking");
System.out.println(" B - Add Recreational Vehicle Booking");
System.out.println(" C - Display Booking Summary");
System.out.println(" D - Update Insurance Status");
System.out.println(" E - Record Recreational Vehicle Weight");
System.out.println(" F - Compile Vehicle Manifest");
System.out.println(" X - Exit");
System.out.print("Enter your selection: ");
String choice = userInput.nextLine().toUpperCase();
user = choice.length() > 0 ? choice.charAt(0) : '\n';
if (choice.trim().toString().length()!=0){
switch (user){
case 'A':
addVehicleBooking();
break;
case 'B':
addRecreationalVehicleBooking();
break;
case 'C':
displayBookingSummary();
break;
case 'D':
break;
case 'E':
break;
case 'F':
break;
case 'X':
break;
default:
break;
}
}
}while(user!='X');
}
}