| Author |
Passing null parameter list in getDeclaredMethod
|
Dennis Putnam
Ranch Hand
Joined: Feb 03, 2012
Posts: 208
|
|
I am trying to use getDeclaredMethod and get a warning (not an error) when I try to pass null for my parameter list. I'd like to eliminate that warning but I can't seem to come up with the right syntax. The warning wants me to explicitly cast to Class<?> but everything I've tried results in an error.
This is the warning code:
This is apparently what I need but can't get right:
Can someone give me the correct syntax? TIA.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32712
|
|
It doesn’t want null. Try an empty array
(Class<?>[])new Object[]{}
instead.
By the way, what is giving you that warning? Is it an IDE?
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32712
|
|
You might not be able to cast that, in which case try
new Class[]{}
You may have to miss out the <?> or you might be able to cast it like this
(Class<?>[])new Class[]{}
You are not supposed to be able to create an array of a parameterised type.
|
 |
Dennis Putnam
Ranch Hand
Joined: Feb 03, 2012
Posts: 208
|
|
|
Thanks for the reply and yes it is an IDE giving me that warning. Your solution (new Class[]{}) fixed it.
|
 |
Mike Simmons
Ranch Hand
Joined: Mar 05, 2008
Posts: 2782
|
|
If you're using JDK 5 or later, you can take advantage of varargs - you don't need to explicitly create an array at all:
Since there are no Class arguments, the compiler inserts code to automatically create an empty array, just as you did.
|
 |
 |
|
|
subject: Passing null parameter list in getDeclaredMethod
|
|
|