samedi 21 novembre 2015

Getting error when using object[] in constructor when using reflections in java

So, the title sais everything. When i try to pass arguments in an Object array using .newInstance i get an error:

java.lang.IllegalArgumentException: argument type mismatch at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) at java.lang.reflect.Constructor.newInstance(Unknown Source) at me.voxed.textrpg.RoomCreator.setNorth(RoomCreator.java:44) at me.voxed.textrpg.Game.(Game.java:89) at me.voxed.textrpg.Program.main(Program.java:6)

This is the function im using:

    public void setNorth(String block, String... args){
    try {
        Object[] argsObj = args;
        Class<? extends Block> clazz = (Class<? extends Block>) (GameRegistry.getBlock(block));
        _north = clazz.asSubclass(Block.class).getConstructor(Object[].class).newInstance(argsObj);
    } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException
            | NoSuchMethodException | SecurityException e) {
        e.printStackTrace();
    }
}

And clazz is this class:

public class BlockNPCSpawnBars extends BlockNPC {

public BlockNPCSpawnBars(Object[] args) {
    super(args);
}

What am i doing wrong?





Aucun commentaire:

Enregistrer un commentaire