gpt4 book ai didi

java - 预订应用程序中的 NullPointerException

转载 作者:行者123 更新时间:2023-11-29 03:33:53 29 4
gpt4 key购买 nike

我在以下位置收到 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');
}
}

最佳答案

您的代码位于:

private static ArrayList<VehicleBooking> bookingList;

还没有初始化。所以初始化它。

关于java - 预订应用程序中的 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16543748/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com