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

  • Home
  • SEARCH
  • 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 8386953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:05:06+00:00 2026-06-09T18:05:06+00:00

I’m making a kind of test program to be able to override methods in

  • 0

I’m making a kind of test program to be able to override methods in classes for an api I’m making in java but I’m getting a weird error when trying to invoke a method from another class…

Here is the main “component class”:

 package st.cmp;

 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;

 public class Component {

public class Overrider{
    Class<?> source;
    Class<?>[] overs;
    String name;
    public Overrider(Class<?> s,String n,Class<?>[] o){
        source=s;
        overs=o;
        name=n;
    }
    public Object call(Object[] param){
        try {
            return source.getMethod(name, overs).invoke(this, param);
        } catch (NoSuchMethodException | SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
};


public HashMap<String,Component> cmps;
public HashMap<String,Overrider> over;

public Component(){
    cmps=new HashMap<String,Component>();
    over=new HashMap<String, Overrider>();
}

public void registerComponent(String nm,Component cm){
    cmps.put(nm,cm);
}
public Component getComponent(String nm){
    return cmps.get(nm);
}

public void override(Class<?> cl,String name,Class<?>[] param){

        over.put(name,new Overrider(cl,name,param));

}

public Object call(String mnm,Object[] a){
    Overrider ov=over.get(mnm);
    if(ov!=null){
        ov.call(a);
    }

    Class<?>[] abc=new Class<?>[a.length];

    for(int i=0;i<a.length;i++){
        abc[i]=a[i].getClass();
    }

        try {
            return this.getClass().getDeclaredMethod(mnm, abc).invoke(this,a);
        } catch (IllegalAccessException | IllegalArgumentException
                | InvocationTargetException | NoSuchMethodException
                | SecurityException e) {
            // TODO Auto-generated catch block
            try {
                this.getClass().getDeclaredMethod(mnm, abc).invoke(this,a);
            } catch (IllegalAccessException | IllegalArgumentException
                    | InvocationTargetException | NoSuchMethodException
                    | SecurityException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }


    return null;
}

public void test(String a){
    System.out.print(a);
}

public int add(Integer a,Integer b){
    return a+b;
}
 }

And this is the main class:

package st;

 import st.cmp.Component;

 public class Start {
public static void main(String[] args)
{
    new Start().start();
}
public void start(){
    Component a=new Component();
    a.call("test",new Object[]{a.call("add",new Object[]{1,5}).toString()});

    a.override(this.getClass(), "add", new Class<?>[]{Integer.class,Integer.class});

    a.call("test",new Object[]{a.call("add",new Object[]{1,5}).toString()});
}
public int add(Integer a,Integer b){
    return a*b;
}

 }

I’m getting this error when I start the program:

6java.lang.IllegalArgumentException: object is not an instance of declaring class
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at st.cmp.Component$Overrider.call(Component.java:22)
    at st.cmp.Component.call(Component.java:64)
    at st.Start.start(Start.java:16)
    at st.Start.main(Start.java:8)
6

Can anyone help me?

it says “object is not an instance of declaring class”… But what “object” is it refering to?

  • 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-06-09T18:05:07+00:00Added an answer on June 9, 2026 at 6:05 pm

    In your Start class, you are calling the Component.override() method:

    //         v--- This is the Class Object
    a.override(this.getClass(), "add", new Class<?>[]{Integer.class,Integer.class});
    

    Where a is of type Component. You are passing it this.getClass(), which is a Class object for Start. Then in override():

    //                          v--- Class object gets passed along here
    over.put(name,new Overrider(cl,name,param));
    

    You are creating a new Overrider, and giving the Class object for Start to the constructor, which sets the Class<?> source; field to the Start Class object. Then when you call the Overrider.call() method, it does this:

    //     v--- and finally invoked here
    return source.getMethod(name, overs).invoke(this, param);
    

    And passes invoke() a this which is an instance of Component, while source is a Class object for Start. In this line, “source” and “this” need to be the same class, but Start and Component aren’t.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making a simple page using Google Maps API 3. My first. One marker
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Seemingly simple, but I cannot find anything relevant on the web. What is the
I want to construct a data frame in an Rcpp function, but when I
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I have thousands of HTML files to process using Groovy/Java and I need to

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.