Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6369537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:49:06+00:00 2026-05-25T00:49:06+00:00

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

  • 0

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
enter image description here

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.printStackTrace();
    }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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T00:49:07+00:00Added an answer on May 25, 2026 at 12:49 am

    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);
            }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Imagine we have following classes: public interface MyInterface<T> { List<T> getList(T t); } abstract
I have a number of classes that register with a notification class in my
I have the following entity: @Entity public class TestCaseStep implements JPAEntity<Integer> { ... @Column(name=STEP_NUMBER)
I have a class library that contains a number of classes. I would like
I have a number of classes that are all related conceptually, but some more-so
I have a number of classes that inherit from one superclass. The superclass as
I have a number of DAO classes that extend SqlMapClientDaoSupport, and call getSqlMapClientTemplate() to
I have a number of static classes that contain tables like this: using System;
I have been reading documentation describing class inheritance, abstract base classes and even python
I have a number of class, all with exactly the same interface. This interface

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.