java - NullPointerException in booking app -
i'm getting nullpointerexception
at:
if(bookinglist.size() == 0)
,
bookinglist.add(vehiclebooking)
and
bookinglist.add(rvbooking)
and not sure causing it. appreciated.
stack trace
bookingsystem.ferrybookingsystem @ 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; (int = 0; < bookinglist.size(); i++){ if(bookinglist.get(i).getbookingid().equals(ids)) return i; } return -1; } public static boolean addvehiclebooking(){ system.out.print("please enter booking id: "); string booking_id = userinput.nextline(); if(bookingidexists(booking_id) != 1) { system.out.println("\nerror - sale id \"" + booking_id + "\" exists in system!"); return false; } system.out.print("please enter registration number vehicle: "); string registration_number = userinput.nextline(); system.out.print("please enter vehicle " + "description: "); string vehicle_description = userinput.nextline(); system.out.print("please enter number of people travelling in 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 " + booking_id); return true; } public static boolean addrecreationalvehiclebooking(){ system.out.print("please enter booking id: "); string booking_id = userinput.nextline(); system.out.print("please enter registration number vehicle: "); string registration_number = userinput.nextline(); system.out.print("please enter vehicl description: "); string vehicle_description = userinput.nextline(); system.out.print("please enter number of people travelling in 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 " + booking_id); return true; } public static void displaybookingsummary(){ if (bookinglist.size() != 0){ system.out.println("\nsummary of past vehicle booking stored on system."); (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(" - 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 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'); } }
move initialization of bookinglist
regular constructor static initialization block:
static { bookinglist = new arraylist<vehiclebooking>(); }
or initialize @ point of declaration:
private static arraylist<vehiclebooking> bookinglist = new arraylist<vehiclebooking>();
otherwise, bookinglist
remain null
until first call of ferrybookingsystem
's constructor made.
Comments
Post a Comment