Unchecked conversion

Viewed 11611

I don't want to supress warnings. Is there another way to handle this warning?

"The expression of type Iterator needs unchecked conversion to conform to Iterator"

Iterator<String> it = schemaJSON.getJSONObject("body").keys();
4 Answers

You can find your answere here: Type Safety warning with JSON Iterator

Iterator<?> it = schemaJSON.getJSONObject("body").keys();

while(it.hasNext())
{
     String next = (String) it.next();
     ...

If you just want to remove the warning,

"The expression of type Iterator needs unchecked conversion to conform to Iterator"

then do this,

Iterator<String> it =  (Iterator<String>)schemaJSON.getJSONObject("body").keys();

but this result in new warning but your old warning is removed.

Type safety: Unchecked cast from Iterator to Iterator<String>

For removing all your warnings,

Iterator<?> it = schemaJSON.getJSONObject("body").keys();    
while(it.hasNext())
{
    String nextElement = (String) it.next();

I think keys() returns an Iterator instance, not an Iterator<String>, so you either leave the String away or just live with the warning ;-)

You can't avoid this warning, because the android JSONObject#keys() returns a 'raw' Iterator. But you can suppress the warnings using the @SuppressWarnings annotation:

@SuppressWarnings("unchecked")
Iterator<String> it = schemaJSON.getJSONObject("body").keys();
Related