0

I am new to java programming and I am learning generics.I tried to do some generics program by myself and I am getting Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Lcom.ashwin.model.Car;.

I have a Vehicle Class:

public class Vehicle {
    private int id;
    private String name;
    private String color;
    private int plateNo;
//omitted getters and setters
}

I have a Car class extending Vehicle Class.

public class Car extends Vehicle {
    
    
  public Car(int id, String name, String color, int plateNo) {
      super.setId(id);
      super.setColor(color);
      super.setPlateNo(plateNo);
  }

}

I have CarDAOImpl.java class:

public class CarDAOImpl implements VehicleDAO<Car> {

        private static ParkingLot<Car> park=new ParkingLot<Car>(10);
        
        
        @Override
        public boolean insert(Car v) {
            
            if(park.getSpace()==-1) {
                return false; 
            }
            else {
                park.setSpace(park.getSpace()-1);
                park.setVehicle(v);
                return true;
            }
        }
    
        @Override
        public boolean delete(Car k) {
            if(park.getSpace()==10) {
                return false;
            }
            else {
             boolean result=park.deleteVehicle(k);
             return result;
            }
            
        }
    
        @Override
        public Car[] getAll() {
            return park.getVehicle();
        }
    
    }

I have another ParkingLot.java class:

public class ParkingLot<T> {
    
    private int space;
    private T[] vehicle;
    
    public ParkingLot() {
        
    }

    public ParkingLot(int sp) {
        this.vehicle=(T[])new Object[sp];   
        this.space=sp;
    }

    public int getSpace() {
        return space;
    }

    public void setSpace(int space) {
        this.space = space;
        
    }

    public  T[] getVehicle() {
        return vehicle;
    }

    public void setVehicle(T vehicle) {
        this.vehicle[space]=vehicle;
    }
    
    public <T extends Vehicle> boolean deleteVehicle(T v) {
        for(int i=0;i<vehicle.length;i++) {
            if(((Vehicle) vehicle[i]).getId()==v.getId()) {
                vehicle[i]=null;
                return true;
            }
        }
        return false;
    }
}

My main method is:

public class Main {

    public static void main(String[] args) {
        VehicleDAO<Car> v=new CarDAOImpl();
        boolean inserted=v.insert(new Car(1,"ford","Red",1234));
        System.out.println(inserted);
        Car[] c=v.getAll();
        for(int i=0;i<c.length;i++)
        {
            System.out.println(c[i]);
        }
        }
}

I am getting error at this line of CarDAOImpl.java class:

@Override
public Car[] getAll() {
    return park.getVehicle();
}

The exception is:

Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Lcom.ashwin.model.Car;
2
  • 1
    Check the warnings the compiler gives you: arrays and generics don't mix well. Stick with collections (such as List) instead and it will work. The cast (T[]) is an unsafe one, so no type safety can be guaranteed and (spoiler alert) you can't get rid of it without some major gymnastics. Just don't mix arrays and generics and you will be better off. Commented Oct 7, 2020 at 12:42
  • I agree with the suggestion of using List, but couldn't you simply return Vehicle[] on the function that throws the error? Commented Oct 7, 2020 at 12:49

1 Answer 1

1

You need to update your constructor to include the class object as a parameter:

public ParkingLot(Class<T> clazz, int sp) {
    this.vehicle= (T[]) Array.newInstance(clazz, sp);
    this.space=sp;
}

And your variable declaration should look like this:

private static ParkingLot<Car> park = new ParkingLot<>(Car.class, 10);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.