samedi 26 février 2022

"java.lang.IllegalArgumentException: java.lang.ClassCastException@4ce4fde9" when trying to clone the subclass of an abstract class using a copy const

Here is the complete cloning process:

void ghost(){
//accesses player hotbar to grab the in-hand object
int hotbarAccessIndex = game.keyManager.lastNumKey;

//variable toClone is the object grabbed from the hotbar
AbstractSprite toClone = game.player.hotbar.inventoryArray[hotbarAccessIndex];

//Class of toClone is grabbed 
Class toClass = toClone.getClass();

//toClass is used to grab toClone's copy constructor
Constructor toConstruct = null;
try{
  toConstruct = toClass.getDeclaredConstructor(P4Torio.class, toClass);
} catch(NoSuchMethodException e){
  print(e);
}

AbstractSprite clone = null;
try{
  //AbstractSprite[] params = {toClone};
  clone = (AbstractSprite) toConstruct.newInstance(P4Torio.class, toClone);
} catch(Exception e){
  print(e + "\n");
}

I get the exception listed in the title when attempting to do the following:

AbstractSprite clone = null;
try{
  //AbstractSprite[] params = {toClone};
  clone = (AbstractSprite) toConstruct.newInstance(P4Torio.class, toClone);
} catch(Exception e){
  print(e + "\n");
}

Here is the copy constructor of toClone:

API(API other){
super(other.x, other.y, other.size, other.direction, other.path, other.tint);

}

Here is the copy constructor of AbstractSprite:

AbstractSprite(AbstractSprite other){
this.x = other.x; this.y = other.y; 
this.w = other.w; this.h = other.h; 
this.size = other.size;
this.direction = other.direction; this.path = other.path;
this.sprite = other.sprite;
this.col = other.col; this.tint = other.tint;

}

Any ideas?





Aucun commentaire:

Enregistrer un commentaire