1

我得到了一个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');
    }
}
4

5 回答 5

2

您的代码位于:

private static ArrayList<VehicleBooking> bookingList;

尚未初始化。所以初始化它。

于 2013-05-14T12:56:56.067 回答
2

bookingList 是 FerryBookingSystem 的静态属性。

您在构造函数中对其进行初始化,这对于静态属性来说是无意义的。

然后,您永远不会调用您的构造函数,因为您永远不会实例化 FerryBookingSystem。

编辑:

在更深入地查看您的代码之后,您似乎首先将 bookingList 声明为静态,然后将所有方法标记为静态以解决编译问题......

我不认为你真的需要这个属性是静态的,所以只需删除你的属性和所有方法上的静态键:

public class FerryBookingSystem {
    private ArrayList<VehicleBooking> bookingList;

然后,在 main 方法的开头实例化一个 FerryBookingSystem:

public static void main (String [] args) throws IOException{
    char user;
    FerryBookingSystem fbs=new FerryBookingSystem();

并调用此实例的方法:

    switch (user){
        case 'A':
            fbs.addVehicleBooking();
            break;
        case 'B':
           fbs.addRecreationalVehicleBooking();
            break;
        case 'C':
           fbs.displayBookingSummary();
于 2013-05-14T12:57:18.847 回答
0

将 的初始化bookingList从常规构造函数移动到静态初始化块:

static {
    bookingList = new ArrayList<VehicleBooking>();
}

或在声明时对其进行初始化:

private static ArrayList<VehicleBooking> bookingList = new ArrayList<VehicleBooking>();

否则,bookingList将一直保留null到第一次调用FerryBookingSystem' 的构造函数。

于 2013-05-14T12:56:37.070 回答
0

您在构造函数中初始化静态字段bookingList并在静态方法中使用它。现在您可以在没有创建该类的实例的情况下调用这些方法。

所以很有可能,bookingList在你调用方法时没有初始化。删除所有静态修饰符或直接初始化字段并删除构造函数。

(我建议删除所有静态修饰符并使用您的类的实例)

于 2013-05-14T12:57:54.367 回答
0

不要在构造函数中初始化静态变量。甚至可以在为该类创建实例之前调用 static。

在您的情况下bookingList是一个静态的,可以在不为此类创建任何对象的情况下调用它。我是这就是 NullPonterException 的原因。

于 2013-05-14T13:03:06.763 回答