• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Passing null parameter list in getDeclaredMethod

 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply and yes it is an IDE giving me that warning. Your solution (new Class[]{}) fixed it.
 
Master Rancher
Posts: 4806
72
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic