0

Why when i try to invoke the method i get:

java.lang.IllegalArgumentException: object is not an instance of declaring class

My code:

Class<?> tWCCamRes = tCLSLoader.loadClass("com.github.sarxos.webcam.WebcamResolution");
Field tVGA = tWCCamRes.getDeclaredField("VGA");

Method tMeth = tVGA.getDeclaringClass().getDeclaredMethod("getSize");
tMeth.invoke(tVGA, (Object[]) null); // Error

In theory I pass the object instance but it failed.

Thanks in advance :)

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
SamYan
  • 1,553
  • 1
  • 19
  • 38

2 Answers2

1

You invoke the getSize method on the field tVGA, but the method is declared on com.github.sarxos.webcam.WebcamResolution.

If you want to invoke an instance method you have to pass the instance as the inovke method's first argument.

If the method doesn't take an argument like com.github.sarxos.webcam.WebcamResolution.getSize() Just invoke it this way:

tMeth.invoke(webcamResolutionObj);

But why don't you just use the WebcamResolution enum.

 String enumName = "VGA";
 WebcamResolution wcResolution = WebcamResolution.valueOf(enumName);
 Dimension size = wcResolution.getSize();
René Link
  • 48,224
  • 13
  • 108
  • 140
  • Do you mean this: tMeth.invoke(tWCCamRes); ? It failed – SamYan Jul 28 '13 at 14:55
  • I can't use directly "WebcamResolution" class, because i'm writting an client / server and i dont want include all libraries in client side app for not increase the size, so the client side application will download and use the libraries when it need. Sorry for my bad english. – SamYan Jul 28 '13 at 15:01
  • No tWCCamRes is the class object. You need an instance of that class to invoke the instance method, but like I answered.. why don't use the enum valueOf Method? – René Link Jul 28 '13 at 15:01
  • I took a look at the library. It is only 382k. You must have very strict memory requirements if you can not add this library to the client application. – René Link Jul 28 '13 at 15:07
  • There are 5 libraries more: bridj-0.6.3-20130316.190111-13.jar dx-1.7.jar logback-core-1.0.13.jar slf4j-api-1.7.2.jar slf4j-nop-1.7.5.jar – SamYan Jul 28 '13 at 15:08
1

You're calling the method getSize(), using reflection, on an object of type Field (tVGA), instead of calling it on the value of this field, which is of type WebcamResolution.

Assuming that you really need to do this via reflection, the code should be:

Class<?> tWCCamRes = tCLSLoader.loadClass("com.github.sarxos.webcam.WebcamResolution");
Field tVGA = tWCCamRes.getDeclaredField("VGA");
Object vgaFieldValue = tVGA.get(null); // it's a static field, so the argument of get() can be null.

Method tMeth = tVGA.getDeclaringClass().getDeclaredMethod("getSize");
tMeth.invoke(vgaFieldValue);
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Thank you very much, it works perfectly but can you explain me why "tVGA.get(null);" ? I'm new in classloader issue, sorry. – SamYan Jul 28 '13 at 15:06
  • `field.get(Object o)` returns the value of the field for the object `o`. But here, the field is a static field. So it doesn't have any object to find the value of the field in. The field value is in the class itself. So the `o` argument is ignored. It's explained in the javadoc of `Field.get()`. – JB Nizet Jul 28 '13 at 15:08
  • It's like you get the instance of class with "tVGA.get(null)" ? It's right ? This happens in all cases when you need to get the instance of the class? – SamYan Jul 28 '13 at 15:10
  • 1
    Let's take a simpler example. You have an `Employee` class with a field `name`. You have two instances of Employee: `john` and `mary`. You want to get the name of mary using reflection. So you get the field "name" from the class `Employee`. And then you ask this field to tell what its value is in the object `mary`. That will return "Mary". Here, the field is a static field. Its value is in the class itself, not in an instance of the class. So you don't need to pass any instance to the `get()` method. `tVGA.get(null)` is the equivalent, without reflection, of `WebcamResolution.VGA`. – JB Nizet Jul 28 '13 at 15:16
  • Now I understand. A very clear explanation! Muchas gracias :) – SamYan Jul 28 '13 at 15:25