BlackBerryForums.com : Your Number One BlackBerry Community      

»Sponsored Links



Reply
 
LinkBack Thread Tools
  (#1 (permalink)) Old
hibbert Offline
Thumbs Must Hurt
 
Posts: 98
Join Date: May 2007
Model: 7290
PIN: N/A
Carrier: private
Default ClassCastException - don't know why - 07-17-2008, 05:18 AM

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


I'm sorry for my bad english
   
Reply With Quote
Sponsored Links
Please Login or Register to Remove these Advertisements!

  (#2 (permalink)) Old
hrbuckley Offline
Thumbs Must Hurt
 
Posts: 175
Join Date: Jan 2006
Model: 8820
Carrier: Rogers
Default 07-17-2008, 07:29 AM

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.
   
Reply With Quote
  (#3 (permalink)) Old
holy3daps Offline
Thumbs Must Hurt
 
Posts: 98
Join Date: Apr 2006
Location: Boston
Model: 8xxx+
Carrier: All Of The Above
Default 07-17-2008, 09:45 AM

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!
   
Reply With Quote
Reply


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-2008 BlackBerryNews.com, BlackBerryFAQ.com, BlackBerryForums.com.
The names RIM © and BlackBerry © are registered Trademarks of Research In Motion Limited.
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.0.1