The moose likes Servlets and the fly likes Java 7 Int to String casting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Java 7 Int to String casting" Watch "Java 7 Int to String casting" New topic

Java 7 Int to String casting

malik ge
Ranch Hand

Joined: May 13, 2011
Posts: 69

In java6 I think that I can do that:

Take the "id" value from session, cast it into string and store it into some String, and then by using Integer.parseInt I can change it into Int

But in Java7 when I do this, it gives an error that:

am I doing it the right way???
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3064

getAttribute returns an instance of type Object. So before casting it directly, its good to check instance of or better you can invoke toString on that instance (but do check for null before invoking toString).

There isn't any changes in Java 7 which would cause such a problem.

And the exception you are seeing might be due to the fact the the getAttribute("id") is actually a Integer and you are trying to cast it to String.

Mohamed Sanaulla | My Blog
malik ge
Ranch Hand

Joined: May 13, 2011
Posts: 69
Miku Ranjan
Ranch Hand

Joined: Oct 11, 2011
Posts: 98
I think you are storing Integer against key id in session right ?
jQuery in Action, 2nd edition
subject: Java 7 Int to String casting
Similar Threads
From object to int
Integer vs. String
Synchronization problem with session?!
one object multiple tables
Switch error: incompatible type found