BlackBerry Forums Support Community               

Closed Thread
 
LinkBack Thread Tools
Old 07-17-2008, 05:18 AM   #1 (permalink)
Thumbs Must Hurt
 
Join Date: May 2007
Location: berlin, germany
Model: 8310
PIN: N/A
Carrier: vodafone
Posts: 163
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default ClassCastException - don't know why

Please Login to Remove!

Hey there,
i've got a problem, and i don't know why... i try to explain my little problem:

i've got a ListField with a listcallback (ListCallback2), a vector (named _messages) of my own Class MyOrder. Now I want to draw all Items of the Vector into the ListField. Here is some of my sourcecode:
MyOrder-Class
PHP Code:
public class MyOrder {
    private 
int status;
    private 
String ursprung;
//... 
mainScreen with the ListCallback and ListField
PHP Code:
private static Vector _messages;
    public static 
ListField _listField;
    public static 
ListCallback2 callback;
//...
    
_listField = new ListField();
    
callback = new ListCallback2();
    
_listField.setCallback(callback);
//...
public static MyOrder getOrder(int id) {
    
MyOrder dummy = (MyOrder)_messages.elementAt(id);
    return 
dummy;
}
//...
private class ListCallback2 implements ListFieldCallback {
        public 
Vector listElements = new Vector();
        
        public 
void drawListRow(ListField list, Graphics gint indexint yint w) {
            
String text "dummy";
            
String text2 = (String)listElements.elementAt(index);
            try {
                
MyOrder myDummy = new MyOrder();
                
myDummy = (MyOrder)getOrder(index); // <--------- error here
                
text2 "OK";
            } catch (
Exception ex) {
                
System.out.println("Fehler:> "+ex);
            }
            
            
text _messages.size() + "|"+_messages.capacity()+"|"+index+"|"+text2;

            
g.drawText(text15y0w);
        } 
In the marked line i get a ClassCastException - Error, but i dont know why...
I tried it with / without (MyOrder) and with / without new MyOrder()

so how do i get the Object from the index i want to?

tahnks hibbert
__________________
My English is so bad, that makes me nobody so quickly after
Offline  
Old 07-17-2008, 07:29 AM   #2 (permalink)
BlackBerry Extraordinaire
 
Join Date: Jan 2006
Model: LEZ10
OS: 10.0.10
Carrier: Rogers CA
Posts: 1,704
Post Thanks: 20
Thanked 77 Times in 68 Posts
Default

I would guess that you are putting an object on the list that is not a subclass of MyOrder. What I would do (other than check the code that loads the list) is change getOrder(int id) to:
Code:
public static MyOrder getOrder(int id) {
    Object obj = _messages.elementAt(id);
    MyOrder dummy = (MyOrder)obj; // <--- put break point here
    return dummy;
}
Put a break point where indicated and inspect obj to find out what its class is. That will (hopefully) suggest the next step.

Last edited by hrbuckley : 07-17-2008 at 07:30 AM.
Offline  
Old 07-17-2008, 09:45 AM   #3 (permalink)
Thumbs Must Hurt
 
Join Date: Apr 2006
Location: Boston
Model: 8900
Carrier: AT&T
Posts: 98
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi!

You might also try:

Code:
Object obj = getOrder(index);
if (obj instanceof MyOrder)
{
    MyOrder order = (MyOrder)obj;
}
else
{
    // show some indication of why it failed
    if (null == obj)
    {
        // you now know that there is no object at index "index"
    }
    else
    {
        String className = obj.getClass().getName();
        // you will now know what the object's class is
        // and hopefully you can track down how it got there
    }
}
This should give you an idea of what's actually going on.

Question: why are you doing the following:

Code:
MyOrder myDummy = new MyOrder();
myDummy = (MyOrder)getOrder(index);
? You're wiping out the "new MyOrder()" in the very next line, so there's no need to create a new one, unless the constructor is caching it somewhere else, which is a rather interesting design for your MyOrder class.

Cheers,

karl
__________________
Karl G. Kowalski
---------------
Owns a RAZR
Develops for BlackBerry
So next phone will be........an iPhone 3G!
Offline  
Closed Thread


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





Copyright 2004-2014 BlackBerryForums.com.
The names RIM and BlackBerry are registered Trademarks of BlackBerry Inc.