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
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:
PHP Code:
public class MyOrder {
int status;
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();
public static MyOrder getOrder(int id) {
MyOrder dummy = (MyOrder)_messages.elementAt(id);
private class ListCallback2 implements ListFieldCallback {
Vector listElements = new Vector();
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;

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
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

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:
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.
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


You might also try:

Object obj = getOrder(index);
if (obj instanceof MyOrder)
    MyOrder order = (MyOrder)obj;
    // show some indication of why it failed
    if (null == obj)
        // you now know that there is no object at index "index"
        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:

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.


Karl G. Kowalski
Owns a RAZR
Develops for BlackBerry
So next phone will iPhone 3G!
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

HP 8620C Sweep Oscillator Mainframe, No Plug-in Module (Parts/Repair)
HP 8620C Sweep Oscillator Mainframe, No Plug-in Module (Parts/Repair) pictureEXFO FTB-5500 PMD Measurement Set, including FTB-400 Mainframe and M2103 Laser
EXFO FTB-5500 PMD Measurement Set, including FTB-400 Mainframe and M2103 Laser  pictureTektronix TM502A Power Mainframe 2-Slot Chassis for TM500 Series Plugins
Tektronix TM502A Power Mainframe 2-Slot Chassis for TM500 Series Plugins pictureApplied Materials Mainframe Interface
Applied Materials Mainframe Interface pictureNardalert S3 Mainframe
Nardalert S3 Mainframe picture

Copyright 2004-2016
The names RIM and BlackBerry are registered Trademarks of BlackBerry Inc.