2

Possible Duplicate:
How do I invoke a private static method using reflection (Java)?

So there is a method named something along the lines of "getInstance" which just returns an instance of a certain class. It's a static method with no arguments.

How could I call that method, and get the return value (the instance) of the class? Every method I try to use requires me to have an instance of the class in the arguments it seems.

For example, I try to use

Method method = classLoader.loadClass("testClass").getMethod("getInstance", null);
            Object object = method.invoke(null, null);

but I always get a null pointer exception at this line,

Object object = method.invoke(null, null);

Which I'm assuming I get since the object it asks for is null.

Thanks for any help.

Edit: Method is not null. I am doing a System.out.println(method == null); and it prints out false.

Community
  • 1
  • 1
Austin
  • 4,801
  • 6
  • 34
  • 54
  • 1
    Have you read javadoc API http://docs.oracle.com/javase/6/docs/api/java/lang/reflect/Method.html#invoke(java.lang.Object, java.lang.Object...) – gigadot Sep 11 '12 at 10:46
  • @gigadot Yes, it says that the first argument in invoke can be null if it's a static method. `If the underlying method is static, then the specified obj argument is ignored. It may be null.` – Austin Sep 11 '12 at 10:48
  • And you're sure that `method` is a non-null value? – zeller Sep 11 '12 at 10:50
  • @zeller Yes, I've done a System.out.println(method == null); and it prints out false – Austin Sep 11 '12 at 10:51
  • 2
    Give us the stacktrace of the NullPointerException. If it ends at that line (and not in the call to invoke), then method must be null, as it is the only thing deferenced on that line. – ILMTitan Sep 11 '12 at 10:54
  • Did you check if you are really entering into that method? I assume you just have one method with that name. – J.A.I.L. Sep 11 '12 at 11:12

5 Answers5

4

You don't want null as your argument or parameter list. Instead you can do

Method method = classLoader.loadClass("testClass").getMethod("getInstance", new Class[0]);
Object object = method.invoke(null, new Object[0]);

or the following as they are varargs methods.

Method method = classLoader.loadClass("testClass").getMethod("getInstance");
Object object = method.invoke(null);
// or works but is perhaps confusing.
Object object = method.invoke(null, null);
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • `method.invoke(null, null)` works for me – TedTrippin Sep 11 '12 at 10:59
  • It is possible its ignored for invoke. I wouldn't include it as it is confusing (it looks like it does something) – Peter Lawrey Sep 11 '12 at 11:03
  • 2
    Confusing or not, it is not the error. For backwards compatibility, varargs is the last thing it tries when resolving a method, null can resolve to Object[], and null is a valid value of args. – ILMTitan Sep 11 '12 at 11:09
  • This answers has been downvoted, and has a comment stating it's not the error. But it's been accepted. @Austin, how did this answer help you solving your problem? – J.A.I.L. Sep 18 '12 at 11:14
  • @J.A.I.L. The answer fixed the problem. The reason for the downvotes was that it suggested two changes when only the first one was required. The second change is preferable IMHO, but optional. – Peter Lawrey Sep 18 '12 at 11:20
1

For me the following two pieces of code both work correctly for static method and print out the returned value.

Method method = myClass.getMethod("getInstance", null);
Object object = method.invoke(null);
System.out.println("returned value: "+object);

as well as

Method method = myClass.getMethod("getInstance", null);
Object object = method.invoke(null,null);
System.out.println("returned value: "+object);

I think that it depend on which Java version you are using (no varargs before 1.5). I'm using Java runtime v1.6.

quickanalysis
  • 416
  • 2
  • 3
0

That is not the problem. If the underlying method is static, then the specified obj argument is ignored. It may be null.

Therefore it seems that method itself must be null.

ILMTitan
  • 10,751
  • 3
  • 30
  • 46
  • Method is not null, I've done a System.out.println(method == null); and it prints out false – Austin Sep 11 '12 at 10:51
0

I bet the problem is in your getInstance method. Can you debug it or post it.

TedTrippin
  • 3,525
  • 5
  • 28
  • 46
  • 1
    But then he would be getting an InvocationTargetException, not a NullPointerException – ILMTitan Sep 11 '12 at 11:02
  • Try writing an example. I have written one that throws a NPE and I get a NPE not a InvocationTargetException. – TedTrippin Sep 11 '12 at 11:05
  • Just tried it. It wrapped both manually and natively thrown NPE in an InvocationTargetException. Are you sure you are not thinking of Class.newInstance()? – ILMTitan Sep 11 '12 at 11:22
-1

With getMethod("getInstance", null) you are saying "get a method with name 'getInstance' and no parameters".

When you type method.invoke(null, null) you are saying "invoke this method on object 'null', and pass 'null' as the first parameter".

You should remove that last null and just type:

Object object = method.invoke(null);
J.A.I.L.
  • 10,644
  • 4
  • 37
  • 50