0

我在以下位置收到 ClassCastException:

RVBooking rvbooking = (RVBooking) bookingList.get(iweight);

我想存储用户的 weightvehicle 输入并调用 recordWeight() 方法。抱歉,如果我没有清楚地解释这一点。提前致谢。

堆栈跟踪

FerryBookingSystem [Java Application]   
    bookingSystem.FerryBookingSystem at localhost:51019 
        Thread [main] (Suspended (exception ClassCastException))    
            FerryBookingSystem.recordVehicleWeight() line: 144  
            FerryBookingSystem.main(String[]) line: 191 
    C:\Program Files\Java\jre7\bin\javaw.exe (16/05/2013 12:42:10 AM)   

预订异常

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 = new ArrayList<VehicleBooking>();
    private static ArrayList<RVBooking> rvbookingList = new ArrayList<RVBooking>();
    private static Scanner userInput = new Scanner (System.in);


    public static int bookingIDExists(String IDs){
       if (bookingList.size() == -1){
             return -1;
       }
          for (int i = 0; i < bookingList.size(); i++){
             if(bookingList.get(i).getbookingID().equals(IDs)){
                return i;
             }
          }
          return -1;
       } 
    public static int rvbookingIDExists(String IDs){
       if (rvbookingList.size() == -1){
          return -1;
       }
          for(int i = 0; i < rvbookingList.size(); i++){
             if(rvbookingList.get(i).getbookingID().equals(IDs)){
                return i;
             }
          }
          return -1;
    }
public static boolean recordVehicleWeight(){
   String booking_ID;
   double weight = 0;
   int iweight = 0;
   System.out.print("Please enter the booking ID: ");
   booking_ID = userInput.nextLine();
   if(bookingIDExists(booking_ID) != -1){
      if(rvbookingIDExists(booking_ID) != 1){
        RVBooking rvbooking = (RVBooking) bookingList.get(iweight);
         System.out.print("Please enter the weight of the vehicle: ");
         weight = userInput.nextDouble();
         iweight = (int)weight;
         rvbooking.recordWeight(iweight);
         return true;
      }
      else{
         System.out.println("Error - cannot record weight for vehicle booking!");
         return false;
      }
   }
   else{
      System.out.println("The booking ID does not exist, Please try again.");
      return false;
   }
}

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':
           updateInsuranceStatus();
            break;
        case 'E':
           recordVehicleWeight();
            break;
        case 'F':
            break;
        case 'X':
            break;
        default:
            break;
            }
    }
    }while(user!='X');
}

}

4

2 回答 2

2

您正在尝试将 RVBooking 类型转换为 VehicleBooking 列表中的对象

于 2013-05-15T14:50:04.193 回答
1

看起来您bookingList的目的是存储类型的对象,并且您在从列表中获取时VehicleBooking尝试将该对象强制转换为RVBooking

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

除非RVBooking是 的超类VehicleBookingVehicleBooking不能转换为RVBooking

于 2013-05-15T14:50:56.370 回答