I have this piece of code
List<Class<? extends SubApplication>> appClasses = new ArrayList<Class<? extends SubApplication>>();
List<SubApplication> subApps = new ArrayList<SubApplication>();
// instructions filling "appClasses"
// ... other instructions
for (Class<? extends SubApplication> crtClass : appClasses) {
try {
Constructor<? extends SubApplication> constructor = crtClass.getConstructor(new Class<?>[] { Application.class });
SubApplication subApp = constructor.newInstance(this);
if (!subApps.contains(subApp)) {
subApps.add(subApp);
}
} catch (SecurityException e) {
} catch (NoSuchMethodException e) {
} catch (IllegalArgumentException e) {
} catch (InstantiationException e) {
System.out.println(e.getMessage());
} catch (IllegalAccessException e) {
} catch (InvocationTargetException e) {
}
}
whereas this is an instance of Application.
I have several implementations of SubApplication. It’s part of the contract that all subclasses of this class need to implement a constructor taking a Application as parameter.
Now I always receive an InstanciationException even though I exactly known that the subclass contains such a constructor. Further details on the exception aren’t available: getMessage() returns null
Edit: SubApplication is abstract. But appClasses only contains sub-classes of SubApplication
Edit2: I had to “construct” the stacktrace, because by contract, I have to catch the exception.
sun.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(InstantiationExceptionConstructorAccessorImpl.java:30)
java.lang.reflect.Constructor.newInstance(Constructor.java:513)
ch.migros.gmaare.tk.auth.gui.AuthenticableApp.instanciateSubApps(AuthenticableApp.java:64)
ch.migros.gmaare.tk.auth.gui.AuthenticableApp.<init>(AuthenticableApp.java:42)
ch.migros.gmaare.tk.voicesuite.VoicesuiteApplication.<init>(VoicesuiteApplication.java:34)
sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
java.lang.reflect.Constructor.newInstance(Constructor.java:513)
java.lang.Class.newInstance0(Class.java:355)
java.lang.Class.newInstance(Class.java:308)
com.vaadin.terminal.gwt.server.ApplicationServlet.getNewApplication(ApplicationServlet.java:82)
com.vaadin.terminal.gwt.server.AbstractApplicationServlet.createApplication(AbstractApplicationServlet.java:978)
com.vaadin.terminal.gwt.server.AbstractApplicationServlet.findApplicationInstance(AbstractApplicationServlet.java:801)
com.vaadin.terminal.gwt.server.AbstractApplicationServlet.service(AbstractApplicationServlet.java:456)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
java.lang.Thread.run(Thread.java:662)
Any idea is appreciated.
As described in the comments. I was dumb enough from not to verify the classes names in my array. Actually, I’ve really added SubApplication to the list. Thanks for your inputs anyway!