Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Type safety: Unchecked cast from Object to List<String>

 
jazy smith
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I have a jsp scriplet where i wrote this code

List<String> user = (List<String>)session.getAttribute("username") ;

but it says " Type safety: Unchecked cast from Object to List<String>" error. Any idea to resolve it ?

thanks in advance
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64701
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please take the time to choose the correct forum for your posts. This forum is for questions on Servlets. For more information, please read this.

This post has been moved to a more appropriate forum.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64701
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Scriptlets? In 2010?

In any case, that's a standard Java warning, indicating that you are casting a non-generic type (Object) to a generic type (List<String>).
 
Keith Flo
Ranch Hand
Posts: 128
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm gonna guess that the 'username' attribute is not a java.util.List?

What is contained in the attribute? ... a String? If 'username' is a String you probably just want to add the string to a List.
Like so ...


 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Java you can remove the warning by using an unchecked annotation; I don't know if that's possible in a JSP or not--never tried. As Bear stated, that kind of code doesn't really belong in a JSP anyway.

@Keith: Not really; Bear is correct on both counts--the scenario you're describing would create an exception at runtime, not compile time, as is happening here.
 
Keith Flo
Ranch Hand
Posts: 128
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David,

Yes ... you're right ... attributes are stored as Objects so the 'username' attribute would need to cast from Object to String .. I overlooked that.

 
jazy smith
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ Bear : thanks for moving the post to appropriate forum. I will keep in my next time before posting the forum.

by the way why were you shocked by saying
Scriptlets? In 2010?
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because Java code in a JSP page is an anachronism--it's a bad practice.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64701
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Scriptlets in a JSP have been outdated for over 8 years now. You should no longer be using them in favor of the JSTL and EL.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic