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 5945975
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:45:25+00:00 2026-05-22T16:45:25+00:00

I am trying to use Reflection in Java but I am getting a weird

  • 0

I am trying to use Reflection in Java but I am getting a weird error. What are the possible issues when I get an error that says:

java.lang.IllegalAccessException: Class com.myapp.core.utils.EventDispatcher can not access a member of class appApp$1 with modifiers "public"
at sun.reflect.Reflection.ensureMemberAccess(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)

I just trying to create my own EventDispatcher class and inside it, the part that I used reflection, which is also the line of code that causes the problem is:

public void dispatchEvent(Event e, String callMethName) {
IEventListener list = ((IEventListener)listeners[i]);
                list.getClass().getMethod(callMethName, Event.class).invoke(list, e);
}

On my main class, I have something that calls addListener which will just add the listener into a list in the EventDispatcher class this way:

try {
obj.addListener("onTestHandler", new MyTestEventListener(){
    @Override
    public void onTestHandler(Event e) {
        System.out.println("hello!");
    }
});
} catch (SecurityException e) {
    e.printStackTrace();
}

So the first parameter that says “onTestHandler” will pass into the EventDispatcher class and eventually as part of the parameter callMethName in the dispatchEvent method, which will call the method dynamically.

The passing of methods and everything is correct. It is the part that has the reflection somehow has problems. It seems to be able to find the method. But for some reason, throws an IllegalAccessException and cannot call the method.

Why is it like this?

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-22T16:45:26+00:00Added an answer on May 22, 2026 at 4:45 pm

    I suspect that the anonymous class (appApp$1) that implements MyTestEventListener has package visibility and that the reflection code is in another package.

    For this code:

    package foo.p1;
    public class Target {
      public static interface Foo {
        public void bar();
      }
    
      public static Foo newFoo() {
        return new Foo() {
          @Override
          public void bar() { 
          }
        };
      }
    }
    

    This code will fail because the runtime type returned by newFoo() is not a public class:

    package foo.p2;
    import foo.p1.Target;
    public class Main {
      public static void main(String[] args) throws Exception {
        Target.Foo foo = Target.newFoo();
        foo.getClass()
            .getMethod("bar")
            .invoke(foo);
      }
    }
    

    This can be overcome by setting the method as accessible:

    Target.Foo foo = Target.newFoo();
    Method m = foo.getClass()
        .getMethod("bar");
    m.setAccessible(true);
    m.invoke(foo);
    

    Or by using the method from the public interface:

    Target.Foo foo = Target.newFoo();
    Target.Foo.class.getMethod("bar")
        .invoke(foo);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For deleting the app data i am trying to use reflection but getting NPE
I'm trying to use reflection to get a property from a class. Here is
Hi I am trying to use C# reflection to call a method that is
I have a problem trying to solve a situation, when I use Java Reflection
I'm trying to use reflection to automatically test that all my linq2sql entities match
Based on http://www.thinkingguy.net/2010/01/localizing-labelfor-in-aspnet-mvc-2.html I'm trying to use reflection to get to a string property
I am trying to use reflection to collect a property from a class that
I am trying to use reflection to create instance of a class. But it
I am trying to use reflection to get all the (settable) properties in my
I am trying use a Java Uploader in a ROR app (for its ease

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.