预订应用程序中的NullPointerException

时间:2013-05-14 12:54:15

标签: java exception null nullpointerexception

我在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)

BookingException

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

FerryBookingSystem

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

5 个答案:

答案 0 :(得分:2)

您的代码:

private static ArrayList<VehicleBooking> bookingList;

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

答案 1 :(得分:2)

bookingList是FerryBookingSystem的静态属性。

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

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

修改

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

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

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();

答案 2 :(得分:0)

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

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

或在声明点初始化它:

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

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

答案 3 :(得分:0)

您正在构造函数中初始化 static 字段bookingList,并在 static 方法中使用它。现在,您可以调用这些方法,而无需创建该类的实例。

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

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

答案 4 :(得分:0)

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

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