×

Discussion Board

Results 1 to 5 of 5
  1. #1
    Regular Contributor
    Join Date
    Oct 2008
    Location
    Bangalore,India
    Posts
    129

    java.lang.classcastexception

    when i want to load the image
    getting img url from vector
    Code:
    //img loading in vector
    -----------------------
    
     String imgUrl = is.readString(length);
            String imgRes = "" + type + "~" + imgUrl;
            resource.addElement(imgRes);        
    
    //and img url  retrieve
    -----------------------
    
    try {
    			urlData = (String)resource.elementAt(imageResourceId);
    		} catch (Exception e) {
    			System.out.println("Exception in getImageResource : " + e);
    			e.printStackTrace();
    		}

    giving java.lang.classcastexception rarely

    not frequently.

    Does type casting to string needs to be handled separately

  2. #2
    Nokia Developer Champion
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    3,073

    Re: java.lang.classcastexception

    when i want to load the image
    getting img url from vector
    Code:
    //img loading in vector
    -----------------------

    String imgUrl = is.readString(length);
    String imgRes = "" + type + "~" + imgUrl;
    resource.addElement(imgRes);

    //and img url retrieve
    -----------------------

    try {
    urlData = (String)resource.elementAt(imageResourceId);
    } catch (Exception e) {
    System.out.println("Exception in getImageResource : " + e);
    e.printStackTrace();
    }

    giving java.lang.classcastexception rarely

    not frequently.

    Does type casting to string needs to be handled separately
    Check if the request elementAt(position) is not null or

    try to print --- resource.elementAt(imageResourceId) if case of exception as to does it contains anything or not.

    thanks,
    ~Amitabh

  3. #3
    Regular Contributor
    Join Date
    Oct 2008
    Location
    Bangalore,India
    Posts
    129

    Re: java.lang.classcastexception

    Hi,

    I have chkd in WTK emulator and s40 emulator.
    Image is displaying in WTK but if it comes to s40 image is not displaying.

    sometimes the above is happening and sometimes classcastexception occuring.

    thanks

  4. #4
    Nokia Developer Champion
    Join Date
    Mar 2003
    Posts
    4,104
    Normally, you do not typecast to String explicitly. Either do
    Code:
    urlData = "" + resource.elementAt(imageResourceId); // implicit typecast; bad coding-style
    or if you sure your value is never null
    Code:
    Object objData = resource.elementAt(imageResourceId);
    urlData = objData.toString(); // explicit conversion
    Again, the latter is bad code because it do not check for null.

  5. #5
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: java.lang.classcastexception

    Quote Originally Posted by kusumat View Post
    Code:
    			urlData = (String)resource.elementAt(imageResourceId);
    If this line throws a ClassCastException, then the element in the Vector is not a String. (null will not throw a ClassCastException, since null can be cast to any Class-type.)

    Try:

    Code:
    Object o = resource.elementAt(imageResourceId);
    System.out.println(o.getClass().getName());
    to see what you actually have.

    Graham.

Similar Threads

  1. Midlet gives java.lang.ClassCastException
    By manojpaul in forum Mobile Java General
    Replies: 9
    Last Post: 2009-06-29, 12:24
  2. MFStandardConnection and java.lang.ClassCastException
    By i12vamor in forum Near Field Communication
    Replies: 22
    Last Post: 2008-06-26, 16:13
  3. java.lang.ClassCastException
    By hardc0d3r in forum Mobile Java General
    Replies: 2
    Last Post: 2007-11-11, 21:27
  4. java.lang.ClassCastException error in Eclipse
    By jacky521 in forum Mobile Java General
    Replies: 3
    Last Post: 2007-10-15, 03:18

Posting Permissions

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