java.lang.NoSuchMethodException from a Class.forName instantiation

Hi @rowanmcnitt,

Would help to see your class structure but the issue is imho that you are trying to instanciate a nested class which constructor is internally not as it looks like, but rather nestedclass(Class Mainclass), even if you adding the default constructor…

ie: if you have such constuct …

public class Mainclass {
  public Mainclass() { }
  public class Nestedclass {
    public Nestedclass() {}
  }
}

the signatures sth like this:

constructors of Mainclass: public Mainclass()
constructors of Mainclass$Nestedclass: public Mainclass$Nestedclass(Mainclass)

you can check that depending on your code…

Class mycls = Class.forName("gun_game_main$" + gun_name);
Constructor[] constructors = mycls.getConstructors();
for(int i = 0; i < constructors.length; i++) {
   System.out.println("constructors of "+mycls.getName()+": " + constructors[i].toString());
}	

Cheers
— mnse

1 Like