1

I'm getting a NullPointerException at:

If(bookingList.size() == 0)

,

bookingList.add(vehicleBooking) 

and

bookingList.add(rvBooking) 

And i am not sure what is causing it. Any help is much appreciated.

Stack trace

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');
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Ken
  • 45
  • 1
  • 5

5 Answers5

2

Your code at:

private static ArrayList<VehicleBooking> bookingList;

have not been initialized. So initialize it.

Wayan Wiprayoga
  • 4,472
  • 4
  • 20
  • 30
2

bookingList is a static attribute of FerryBookingSystem.

You initialize it in the constructor, which is a non-sense for a static attribute.

Then, you never call your constructor because you never instantiate FerryBookingSystem.

Edit:

After looking more deeply into your code, it seems that you first declared bookingList as static then marked all methods static to solve compilations problems...

I don't think that you really need this attribute to be static so just remove the static keywork on your attribute and on all your methods:

public class FerryBookingSystem {
    private ArrayList<VehicleBooking> bookingList;

Then, instantiate a FerryBookingSystem at the beginning of your main method:

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

and call methods of this instance :

    switch (user){
        case 'A':
            fbs.addVehicleBooking();
            break;
        case 'B':
           fbs.addRecreationalVehicleBooking();
            break;
        case 'C':
           fbs.displayBookingSummary();
Arnaud Denoyelle
  • 29,980
  • 16
  • 92
  • 148
0

Move initialization of the bookingList from a regular constructor to a static initialization block:

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

or initialize it at the point of declaration:

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

Otherwise, bookingList would remain null until the first call of FerryBookingSystem's constructor is made.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

You're initializing the static field bookingList in a constructor and use it in static methods. Now you can call those methods without haveing created an instance of that class.

So there is a good chance, that bookingList is not initialized at the time you call the methods. Either remove all static modifiers or initialize the field directly and drop the constructor.

(I recommend removing all static modifiers and use instances of your class)

Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
0

Don't initialize the static variables in a constructor. static can be called even before instance created for that class.

In your case bookingList is a static and it can be called without creating any object for this class. My be this is the reason for NullPonterException.

Satya
  • 8,146
  • 9
  • 38
  • 43