Note that there are some explanatory texts on larger screens.

plurals
  1. POError instantiating a class using class.forname().newInstance()
    text
    copied!<p>I have a number of classes and interface that are in the following structure</p> <pre><code>interface Parser{} public class ParserA implements Parser{} public class ParserB implements Parser{} public class ParserC implements Parser{} public abstract class GenericParser implements Parser{} public abstract class GenericXMLParser extends GenericParser{} public class ParserD extends GenericXMLParser{} public class ParserE extends GenericXMLParser{} public class ParserF extends GenericXMLParser{} </code></pre> <p>The classes are in different packages. Here is the package structure for the above classes. </p> <pre><code>com.app.parsers.Parser com.app.parsers.ParserA com.app.parsers.ParserB com.app.parsers.ParserC com.app.parsers.xml.GenericParser com.app.parsers.xml.GenericXMLParser com.app.parsers.xml.ParserD com.app.parsers.xml.ParserE com.app.parsers.xml.ParserF </code></pre> <p>I am trying to create an instance of each of the following classes using class.forname() (Not sure what the technical name is for this approach)</p> <pre><code>com.app.parsers.ParserA com.app.parsers.ParserB com.app.parsers.ParserC com.app.parsers.xml.ParserD com.app.parsers.xml.ParserE com.app.parsers.xml.ParserF </code></pre> <p>To do this, i put all the class names in a vector and loop through it as shown below:</p> <pre><code> try{ while (tokens.hasMoreTokens()) parsers.addElement(Class.forName((String) tokens.nextToken()).newInstance()); }catch(NullPointerException e){ e.printStackTrace(); }catch(Throwable e){ e.printStackTrace(); } </code></pre> <p>The problem i am having is that the newInstance() call only works for the first 3 classes, i.e. </p> <pre><code>com.app.parsers.ParserA com.app.parsers.ParserB com.app.parsers.ParserC </code></pre> <p>When it tries to create an instance of any of the other classes which extend GenericXMLParser and GenericParser, i get the following error:</p> <pre><code>com.sun.jdi.InvocationException occurred invoking method. </code></pre> <p>The problem i have is when i run the application i dont see the above error. I only see it when i debug the application and it happens when newInstance() is called. I dont understand what could be causing it because the program is running fine if i dont run it in debug mode. I also dont understand why newInstance() works for the classes that dont extend any of the Generic classes. </p> <p>The other thing is that the InvocationException is not being caught by the Throwable catch handler in the above code so it is very difficult to really know what is causing it. None of the classes have any user defined constructors so i am really stuck as to what is causing it. </p> <h2>Edit 1</h2> <p>Oh and i forgot to mention that the above is being compiled and run on JDK 1.4.1_07</p> <h2>Edit 2</h2> <p>Here is a screenshot showing the error on the debugger <img src="https://i.stack.imgur.com/re2kS.jpg" alt="enter image description here"></p> <h2>Edit 3</h2> <p>@Andrew Young , i had to modify your example to make it JDK 1.4.1 as that is the JDK version i am using. i removed the generics and enhanced for loop constructs as shown below:</p> <pre><code> StringTokenizer tokens = new StringTokenizer(parserList); try{ while (tokens.hasMoreTokens()) { String className = tokens.nextToken(); Class klass = Class.forName(className); if(!java.lang.reflect.Modifier.isAbstract(klass.getModifiers())) { java.lang.reflect.Constructor[] constructors = klass.getConstructors(); if(constructors.length &gt; 0) { boolean foundDefaultConstructor = false; for(int i = 0; i&lt;constructors.length; i++) { if(constructors[i].getParameterTypes().length == 0) { foundDefaultConstructor = true; break; } } if(foundDefaultConstructor) { Object parser = klass.newInstance(); parsers.addElement(parser); } else { System.err.println("No Default Constructor: " + className); } } else { System.err.println("No Public Constructors: " + className); } } else { System.err.println("Class is abstract: " + className); } } }catch(NullPointerException e){ e.printStackTrace(); }catch(Throwable e){ e.printStackTrace(); } </code></pre> <p>I tried to run the above and in all cases foundDefaultConstructor is always set to true and it continues to this if block then go loops again for the next one. </p> <pre><code>if(foundDefaultConstructor) { Object parser = klass.newInstance(); parsers.addElement(parser); } </code></pre> <h2>Edit 4</h2> <p>Ok i tried to check the suggestion made by @biziclop I changed one of the parsers and overriden its toString method and it now just returns a simple random String. Now when when debug the application on eclipse i dont see the error on the Parser with the overriden toString() method. It sounds like it is something to do with the toString() method but what exactly i am not sure. </p> <p>The other thing is that the parsers are now showing differently when i click on its variable on the debugger</p> <p>The ones that have always worked always show the value </p> <pre><code>ParserA@3fa5ac </code></pre> <p>The ones that didnt work show the value </p> <pre><code>com.sun.jdi.InvocationException occurred invoking method. </code></pre> <p>The one that didnt work before but now has an overriden toString() method shows the value</p> <pre><code>"in toString" </code></pre> <p>The above is just the value returned from the overriden toString() method. For it to have instantiated correctly should it not have shown as something like ParserE@3fa5ac. I am not sure yet whether the object was instantiated or not with the overriden method. What could be causing this different behaviour with the toString() method. </p> <p>I am now going through the existing parsers to see if any of them does override toString(). </p> <p>Thanks Thanks</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload