BlackBerry Forums Support Community
              

Closed Thread
 
Thread Tools
Old 09-16-2009, 08:10 AM   #1
Cilraaz
Knows Where the Search Button Is
 
Cilraaz's Avatar
 
Join Date: Apr 2008
Location: New Cumberland, PA
Model: 9530
OS: 5.0.0.732
Carrier: Verizon
Posts: 34
Default ClassCastException During Persistent Storage Retrieval

Please Login to Remove!

I've been banging my head off a wall for hours now trying to figure out what's wrong with my code, so now I'm posting here hoping that someone more knowledgeable than I will see my error.

I am creating an app that stores user input in a PersistentObject. Originally, I had each of my four screens inside the main class, but decided that was getting a bit too messy. I've since separated the code to give the main app and each of the four screens their own classes (maintained in separate .java files also). The code worked for both storage and retrieval before the code separation, which is why I'm so perplexed now. In any case, my code is below:

My main application class:
Code:
public class BowlTracker extends UiApplication {

    public static void main(String[] args) {
        BowlTracker bowltracker = new BowlTracker();
        bowltracker.enterEventDispatcher();
    }

    public BowlTracker() {
        HomeScreen homeScreen = new HomeScreen();
        this.pushScreen(homeScreen);
    }

}
My data input class:
Code:
public class InfoInput extends MainScreen {
    
    private static Vector _data;
    private static PersistentObject store;
    private static long storeKey = 0xe4fd484b15f998faL;

    public InfoInput() {
        String[] surfaceType = { "Synthetic", "Wood", "Other" };
        this.setTitle(new LabelField("Info Input Screen", LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH));
        final EditField name = new EditField("Game Name: ", "", 20, EditField.FILTER_DEFAULT);
        final EditField score = new EditField("Score: ", "", 3, EditField.FILTER_NUMERIC);
        final DateField date = new DateField("Date: ", System.currentTimeMillis(), DateFormat.getInstance(DateFormat.DATE_DEFAULT));
        final EditField house = new EditField("Bowling Alley: ", "", 20, EditField.FILTER_DEFAULT);
        final EditField league = new EditField("League Name: ", "", 20, EditField.FILTER_DEFAULT);
        final EditField lane = new EditField("Lane Number: ", "", 3, EditField.FILTER_NUMERIC);
        final ObjectChoiceField laneSurface = new ObjectChoiceField("Lane Surface: ", surfaceType);
        final EditField ball = new EditField("Ball Used: ", "", 20, EditField.FILTER_DEFAULT);
        final EditField notes = new EditField("Notes: ", "", 255, EditField.FILTER_DEFAULT);
        ButtonField submit = new ButtonField("Submit", ButtonField.CONSUME_CLICK);

        this.add(name);
        this.add(score);
        this.add(date);
        this.add(new SeparatorField());
        this.add(league);
        this.add(house);
        this.add(lane);
        this.add(laneSurface);
        this.add(ball);
        this.add(new SeparatorField());
        this.add(notes);
        this.add(new SeparatorField());
        this.add(submit);

        submit.setChangeListener(new FieldChangeListener() {
            public void fieldChanged(Field field, int context) {
                StoreInfo info = new StoreInfo();
                info.setElement(StoreInfo.NAME, name.getText());
                info.setElement(StoreInfo.SCORE, score.getText());
                info.setElement(StoreInfo.DATE, String.valueOf(date.getDate()));
                info.setElement(StoreInfo.LEAGUE, league.getText());
                info.setElement(StoreInfo.HOUSE, house.getText());
                info.setElement(StoreInfo.LANE, lane.getText());
                info.setElement(StoreInfo.SURFACE, laneSurface.getChoice(laneSurface.getSelectedIndex()).toString());
                info.setElement(StoreInfo.BALL, ball.getText());
                info.setElement(StoreInfo.NOTES, notes.getText());
                _data.addElement(info);
                synchronized (store) {
                    store.setContents(_data);
                    store.commit();
                }
                        
                Dialog.inform("Game data stored!");
                name.setText("");
                score.setText("");
                league.setText("");
                house.setText("");
                lane.setText("");
                ball.setText("");
                notes.setText("");
                UiApplication.getUiApplication().popScreen();
            }
        });
    }

    static {
        store = PersistentStore.getPersistentObject(storeKey);
        synchronized (store) {
            if (store.getContents() == null) {
                store.setContents(new Vector());
                store.commit();
            }
        }
        _data = new Vector();
        _data = (Vector) store.getContents();
    }

    private static final class StoreInfo implements Persistable {
        private Vector _elements;
        public static final int NAME = 0;
        public static final int SCORE = 1;
        public static final int DATE = 2;
        public static final int LEAGUE = 3;
        public static final int HOUSE = 4;
        public static final int LANE = 5;
        public static final int SURFACE = 6;
        public static final int BALL = 7;
        public static final int NOTES = 8;
        
        public StoreInfo() {
            _elements = new Vector(9);
            for (int i = 0; i < _elements.capacity(); ++i) {
                _elements.addElement(new String(""));
            }
        }

        public void setElement(int id, String value) {
            _elements.setElementAt(value, id);
        }
    }

}
My data retrieval code:
Code:
public class InfoViewer extends MainScreen {
    
    private static Vector _data;
    private static PersistentObject store;
    private static long storeKey = 0xe4fd484b15f998faL;

    public InfoViewer() {
        String[] surfaceType = { "Synthetic", "Wood", "Other" };
        this.setTitle(new LabelField("Info Viewer Screen"));
        EditField name = new EditField("Game Name: ", "", 20, EditField.READONLY);
        EditField score = new EditField("Score: ", "", 3, EditField.READONLY);
        DateField date = new DateField("Date: ", System.currentTimeMillis(), DateFormat.getInstance(DateFormat.DATE_DEFAULT));
        EditField house = new EditField("Bowling Alley: ", "", 20, EditField.READONLY);
        EditField league = new EditField("League Name: ", "", 20, EditField.READONLY);
        EditField lane = new EditField("Lane Number: ", "", 3, EditField.READONLY);
        ObjectChoiceField laneSurface = new ObjectChoiceField("Lane Surface: ", surfaceType, 0, ObjectChoiceField.READONLY);
        EditField ball = new EditField("Ball Used: ", "", 20, EditField.READONLY);
        EditField notes = new EditField("Notes: ", "", 255, EditField.READONLY);

        String dateString = "";

        int count = _data.size();
        Dialog.inform("Total records stored: "+count);

        synchronized (store) {
            Vector _data = (Vector) store.getContents();
            if(!_data.isEmpty()) {
                String[] gameList = new String[count];
                for (int i = 0; i < count; ++i) {
                    StoreInfo info = (StoreInfo) _data.elementAt(i);  // This is the line of code that throws the exception
                    gameList[i] = info.getElement(StoreInfo.NAME)+" - "+info.getElement(StoreInfo.SCORE);
                }

                ObjectListField gamesInfo = new ObjectListField();
                gamesInfo.set(gameList);
                this.add(gamesInfo);

            } else {
                name.setText("");
                score.setText("");
            }
        }
    }

    static {
        store = PersistentStore.getPersistentObject(storeKey);
        synchronized (store) {
            if (store.getContents() == null) {
                store.setContents(new Vector());
                store.commit();
            }
        }
        _data = new Vector();
        _data = (Vector) store.getContents();
    }

    private static final class StoreInfo implements Persistable {
        private Vector _elements;
        public static final int NAME = 0;
        public static final int SCORE = 1;
        public static final int DATE = 2;
        public static final int LEAGUE = 3;
        public static final int HOUSE = 4;
        public static final int LANE = 5;
        public static final int SURFACE = 6;
        public static final int BALL = 7;
        public static final int NOTES = 8;
        
        public StoreInfo() {
            _elements = new Vector(9);
            for (int i = 0; i < _elements.capacity(); ++i) {
                _elements.addElement(new String(""));
            }
        }
        
        public String getElement(int id) {
            return (String) _elements.elementAt(id);
        }

    }

}
For some reason, it's throwing the ClassCastException when I try to pull an instance of StoreInfo. If anyone can see what I'm doing wrong, I would greatly appreciate it.
Offline  
Old 09-16-2009, 10:29 AM   #2
Cilraaz
Knows Where the Search Button Is
 
Cilraaz's Avatar
 
Join Date: Apr 2008
Location: New Cumberland, PA
Model: 9530
OS: 5.0.0.732
Carrier: Verizon
Posts: 34
Default

Ignore the above. Apparently I only needed 2 more hours to figure it out.

Because I was defining StoreInfo in both InfoInput and InfoViewer, the data object was specific to the class it was defined in (hence the ClassCastException). I've since moved StoreInfo to its own file as a public class and it works fine.
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

UEI DL369 Electrical Clamp/Multi-Meter Voltage Tester Electrician HVAC"No Leads"
$30.0
UEI DL369 Electrical Clamp/Multi-Meter Voltage Tester Electrician HVAC70Pcs 1W 1 Watt Voltage Stabilizing Diode Package 3.3V-30V 14 Common Voltages
$8.35
70Pcs 1W 1 Watt Voltage Stabilizing Diode Package 3.3V-30V 14 Common Voltages pictureDc 3.7-6V 1-3A 400KV Pulse High Voltage Generator Inverter Transformer Pwm Boost
$8.49
Dc 3.7-6V 1-3A 400KV Pulse High Voltage Generator Inverter Transformer Pwm Boost picture12V 24V Mini Red LED Digital Display Volt Meterr Vehicle Voltage Meter Tester Wi
$8.27
12V 24V Mini Red LED Digital Display Volt Meterr Vehicle Voltage Meter Tester Wi pictureGY-287.8oz8511 Uvb Rays Sensor Breakout Test Module uv Detector Analog Voltage O
$8.71
GY-287.8oz8511 Uvb Rays Sensor Breakout Test Module uv Detector Analog Voltage O picture






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