Note that there are some explanatory texts on larger screens.

plurals
  1. POJava: NoSuchMethodException when method clearly exists
    primarykey
    data
    text
    <p>On my current project, I've felt the need to create a sort of simulated callback system in Java using reflection. However, I'm having issues getting my reflection to actually function. The code at fault follows:</p> <pre><code>public Callback(Object parentObj, String methodName, Class&lt;?&gt;...parameters) { if(parentObj == null) throw new IllegalArgumentException("parentObj cannot be null", new NullPointerException()); Class&lt;?&gt; clazz = parentObj.getClass(); // Trace debugging, see output for(Method m : clazz.getDeclaredMethods()) if(m.getName().equals("myMethod")) System.out.println (m); try { this.method = clazz.getMethod(methodName, parameters); } catch(NoSuchMethodException nsme) { nsme.printStackTrace(); } // Exception caught catch(SecurityException se) { se.printStackTrace(); } this.parentObj = parentObj; this.parameters = parameters; } </code></pre> <p>When I construct the <code>Callback</code> object, I'm using syntax like this:</p> <pre><code>new Callback(this, "myMethod", boolean.class) </code></pre> <p>When I try to create my pseudo-callback, it hits the <code>NoSuchMethodException</code> catch block. I've included some trace debugging above to show the output of one of my methods failing. The output:</p> <pre><code>private void my.package.MyClass.myMethod(boolean) java.lang.NoSuchMethodException: my.package.MyClass.myMethod(boolean) at java.lang.Class.getMethod(Class.java:1605) at my.package.other.Callback.&lt;init&gt;(Callback.java:63) </code></pre> <p>I couldn't figure the problem out, so I started hunting, to little avail. The best I could find was mention of versioning conflict between the compiled JAR and the runtime. However, <code>MyJar.jar/META-INF/MANIFEST.MF</code> contains <code>Created-By: 1.6.0_02 (Sun Microsystems Inc.)</code>. My IDE is running <code>C:\Program Files\Java\jdk1.6.0_02\bin\javac.exe</code> to compile my project. I'm using <code>C:\Program Files\Java\jdk1.6.0_02\bin\java.exe</code> to run my JAR.</p> <p>I'm at a loss why <code>Class.getMethod</code> is claiming the method doesn't exist, but <code>Class.getMethods</code> seems to have no problem finding it. Help? :(</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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