reflection: noSuchMethodException

M

Matthijs Blaas

I try to invoke a method (setIgnoreRepaint) only if it's available on the
used JVM through reflection. Problem is that it throws me a
java.lang.NoSuchMethodException:
java.awt.Component.setIgnoreRepaint(java.lang.Boolean) exception even on
JVM's where it is available... I cant figure out what im doing wrong.

An example can be found at: http://www.virtual-boy.org/applet/refl.html (you
can see the error at the java console)

Sources:
http://www.virtual-boy.org/applet/refl.java
 
A

Andrew Thompson

I try to invoke a method (setIgnoreRepaint) only if it's available on the
used JVM through reflection. Problem is that it throws me a
java.lang.NoSuchMethodException:
java.awt.Component.setIgnoreRepaint(java.lang.Boolean) exception even on
JVM's where it is available... I cant figure out what im doing wrong.

You are using Boolean, rather than boolean.

<sscce>
import java.applet.*;
import java.lang.reflect.Method;

public class Refl extends Applet {

public void start() {
try {
Class ob = Class.forName("java.awt.Component");
Method method = ob.getMethod("getComponentAt",
new Class[]{Class.forName("java.awt.Point")} );

ob = Class.forName("java.awt.Component");
method = ob.getMethod("setIgnoreRepaint",
new Class[]{Class.forName("java.lang.Boolean")} );
}
catch(Exception e) {
e.printStackTrace();
}
}
}
</sscce>

As to how you represent a boolean in such cases, that is
left as an exercise for the OP. (No, I have no idea.)
 
M

Matthijs Blaas

As to how you represent a boolean in such cases, that is
Yep, that's it. That works here just fine.

Great, I have it working now! I'd never guessed I had to use the TYPE
field... is that static class used by the Boolean class itself for actual
initialisation of an boolean object?
 
O

Oscar kind

Matthijs Blaas said:
I'd never guessed I had to use the TYPE
field... is that static class used by the Boolean class itself for actual
initialisation of an boolean object?

Almost. The TYPE field denotes a special Class instance describing a
primitive type. AFAIK it is not used for initialization, as primitive
types are not subclasses of Object.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top