Results 1 to 5 of 5
  1. #1
    nn12 is offline Member
    Join Date
    Sep 2008
    Posts
    85
    Rep Power
    0

    Default Object to Int casting

    In my java page I have

    request.setAttribute("intLevel", intLevel); // intLevel is set as an Integer

    and I am retrieving it in my jsp page using

    int level = (int)request.getAttribute("intLevel");

    I get a cast class exception, saying Object cannot be casted into Integer. Can you please help.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    if it is returning an Integer, then can you just leave off the cast and let it auto-unbox to an int? Either that or call intValue() on the returned Integer object?

  3. #3
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    7

    Default

    I suppose that should work
    Java Code:
    int level=Integer.parseInt(request.getAttribute("intLevel").toString);

  4. #4
    nn12 is offline Member
    Join Date
    Sep 2008
    Posts
    85
    Rep Power
    0

    Default

    Thanks guys, I will try those 2 options

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Edit: serjant's recommendation is much better than mine. Go with it.

Similar Threads

  1. type casting
    By alvations in forum New To Java
    Replies: 1
    Last Post: 10-13-2008, 08:07 PM
  2. class casting
    By vital parsley in forum New To Java
    Replies: 1
    Last Post: 07-24-2008, 11:03 AM
  3. Casting problem
    By John_28 in forum New To Java
    Replies: 6
    Last Post: 05-14-2008, 12:29 PM
  4. Type Casting Help
    By rhm54 in forum New To Java
    Replies: 2
    Last Post: 02-07-2008, 01:06 PM
  5. Casting
    By leebee in forum New To Java
    Replies: 5
    Last Post: 08-10-2007, 01:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •