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



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


Numatics Manifold Block w/ (5) Solenoid Valves 061BB400M picture

Numatics Manifold Block w/ (5) Solenoid Valves 061BB400M

$150.00



Solenoid, picture

Solenoid,

$22.86



1/2 in 12V DC Brass Electric Solenoid Valve NPT Gas Water Air Normally Closed US picture

1/2 in 12V DC Brass Electric Solenoid Valve NPT Gas Water Air Normally Closed US

$23.11



2PCS 1/4

2PCS 1/4" Fast Response Electric Solenoid Valve Air Water DC 12V Normally Closed

$18.99



DORMEYER 2005-M-1 Solenoid,Laminated,1/8 - 1 in,Continuous 4X240 DORMEYER 2005-M picture

DORMEYER 2005-M-1 Solenoid,Laminated,1/8 - 1 in,Continuous 4X240 DORMEYER 2005-M

$37.87



3/4

3/4" Brass Electric Solenoid Valve 110V 120V Volt AC Water Air Gas VITON NC B21

$36.20







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