aspose file tools*
The moose likes JSP and the fly likes Type safety: Unchecked cast from Object to List<String> Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Type safety: Unchecked cast from Object to List<String>" Watch "Type safety: Unchecked cast from Object to List<String>" New topic
Author

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

jazy smith
Ranch Hand

Joined: Nov 18, 2009
Posts: 101
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

Joined: Jan 10, 2002
Posts: 60056
    
  65

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.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60056
    
  65

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

Joined: Nov 29, 2005
Posts: 128

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 ...



kf
SCJP 5.0 (preparing for SCWCD)
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Nov 29, 2005
Posts: 128

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

Joined: Nov 18, 2009
Posts: 101
@ 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

Joined: Sep 29, 2008
Posts: 12617

Because Java code in a JSP page is an anachronism--it's a bad practice.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60056
    
  65

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Type safety: Unchecked cast from Object to List<String>
 
Similar Threads
generics compiler warning
unchecked cast exception from object to String[] conversion
Type Casting Generics
Unchecked cast
Type safety: Unchecked cast from Object to List<String>