4 Replies - 10292 Views - Last Post: 03 April 2012 - 03:33 PM Rate Topic: -----

#1 PinguTrace   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 31-March 12

warning: [unchecked] unchecked cast. When trying to cast ArrayList<

Posted 01 April 2012 - 06:44 AM

I am trying to get information from a text file and store it back in an arrayList<String> then return this out of the method.

 ArrayList<String> back = new ArrayList<String>(); 

FileInputSteam a = new FileInputStream(filename);
ObjectInputStream b = new ObjectInputStream(a);

back = (ArrayList<String>) b.readObject();

a.close();

return back;



but I get the error warning [unchecked] unchecked cast
required ArrayList<String>
found Object


on line 6.

What am I doing wrong?

Is This A Good Question/Topic? 0
  • +

Replies To: warning: [unchecked] unchecked cast. When trying to cast ArrayList<

#2 CasiOo   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1578
  • View blog
  • Posts: 3,551
  • Joined: 05-April 11

Re: warning: [unchecked] unchecked cast. When trying to cast ArrayList<

Posted 01 April 2012 - 07:08 AM

It is only a warning. But you should check the type of the object you have just read before assigning it to back. Sadly I don't believe you can check on generic types by using instanceof, you can only check if it is an ArrayList.
Was This Post Helpful? 0
  • +
  • -

#3 Sinned   User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 208
  • Joined: 13-October 10

Re: warning: [unchecked] unchecked cast. When trying to cast ArrayList<

Posted 01 April 2012 - 07:09 AM

This is a bit a strange error, because other casts works fine.
But I think the problem lies partly in the compiler and partly in the running.

Compiler warnings can always be ignored, errors can't. But this is a warning, so add this to the top of you function:
@SuppressWarnings("unchecked")

This handles the compiler part.

To solve the runtime part - I mean you don't want to let your program crash while running because it is not an ArrayList - use the following:
Object o = b.readObject();
if(o instanceof ArrayList)
	back = (ArrayList<String>) o;

This makes sure the Object is an ArrayList. (You can't check the type of the ArrayList)

I hope this helped you,

Sinned
Was This Post Helpful? 3
  • +
  • -

#4 PinguTrace   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 31-March 12

Re: warning: [unchecked] unchecked cast. When trying to cast ArrayList<

Posted 03 April 2012 - 03:26 PM

thankyou so much, the suppress warnings seemed to work. so thank you :)
Was This Post Helpful? 0
  • +
  • -

#5 CasiOo   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1578
  • View blog
  • Posts: 3,551
  • Joined: 05-April 11

Re: warning: [unchecked] unchecked cast. When trying to cast ArrayList<

Posted 03 April 2012 - 03:33 PM

View PostPinguTrace, on 03 April 2012 - 03:26 PM, said:

thankyou so much, the suppress warnings seemed to work. so thank you :)


dont just suppress it without any checks :S
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1