开发者

Error instantiating a class using class.forname().newInstance()

I have a number of classes and interface that are in the following structure

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{}

The classes are in different packages. Here is the package structure for the above classes.

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

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)

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

To do this, i put all the class names in a vector and loop through it as shown below:

    try{
        while (tokens.hasMoreTokens())
            parsers.addElement(Class.forName((String) tokens.nextToken()).newInstance());
    }catch(NullPointerException e){
        e.printStackTrace();
    }catch(Throwable e){
        e.printStackTrace();
    }

The problem i am having is that the newInstance() call only works for the first 3 classes, i.e.

com.app.parsers.ParserA
com.app.parsers.ParserB
com.app.parsers.ParserC

When it tries to create an instance of any of the other classes which extend GenericXMLParser and GenericParser, i get the following error:

com.sun.jdi.InvocationException occurred invoking method.

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.

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.

Edit 1

Oh and i forgot to mention that the above is being compiled and run on JDK 1.4.1_07

Edit 2

Here is a screenshot showing the error on the debugger

Error instantiating a class using class.forname().newInstance()

Edit 3

@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:

    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 > 0) {
                    boolean foundDefaultConstructor = false;
                    for(int i = 0; i<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.printSta开发者_开发知识库ckTrace();
    }catch(Throwable e){
        e.printStackTrace();
    }

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.

if(foundDefaultConstructor) {
                            Object parser = klass.newInstance();
                            parsers.addElement(parser);
                        }  

Edit 4

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.

The other thing is that the parsers are now showing differently when i click on its variable on the debugger

The ones that have always worked always show the value

ParserA@3fa5ac

The ones that didnt work show the value

com.sun.jdi.InvocationException occurred invoking method.

The one that didnt work before but now has an overriden toString() method shows the value

"in toString"

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.

I am now going through the existing parsers to see if any of them does override toString().

Thanks Thanks


The reason you're only seeing it when you debug the application is that this error is thrown by the Java Debug Interface. Trying pulling the nextToken call out of that line like this to see which part is causing the problem:

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 > 0) {
                boolean foundDefaultConstructor = false;
                for(java.lang.reflect.Constructor<?> constructor: constructors) {
                    if(constructor.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);
        }
}


Could it be that you are trying to instantiate your abstract classes?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜