Persistent Store problem, need your help! ~

Forgive and I would like to make a short story.

Days, I am according to the A13_Storing_Persistent_Data_V2 document and fantasize for only a class useful for my application, but I am failed completely, here is that the source code written in the document.

import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.system.*;
import net.rim.device.api.util.*;
import java.util.*; 

public class MusicStores extends UiApplication { 

  private AutoTextEditField namefield;
  private AutoTextEditField addressfield;
  private EditField phonefield;
  private EditField specialtyfield;
  private static Vector _data;
  private static PersistentObject store; 

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

  public MusicStores() {
    MainScreen mainScreen = new MainScreen();
    mainScreen.setTitle(new LabelField("Favourite Music Store"));
    namefield = new AutoTextEditField("Store Name: ", "");
    addressfield = new AutoTextEditField("Address: ", "");
    phonefield = new EditField("Phone Number: ", "", Integer.MAX_VALUE, BasicEditField.FILTER_PHONE);
    specialtyfield = new EditField("Music Type: ", "", Integer.MAX_VALUE, BasicEditField.FILTER_DEFAULT);
    mainScreen.add(namefield);
    mainScreen.add(addressfield);
    mainScreen.add(phonefield);
    mainScreen.add(specialtyfield);
    mainScreen.addMenuItem(saveItem);
    mainScreen.addMenuItem(getItem);
    pushScreen(mainScreen);
  } 

  private MenuItem saveItem = new MenuItem("Save", 110, 10) {
    public void run() {
      StoreInfo info = new StoreInfo();
      info.setElement(StoreInfo.NAME, namefield.getText());
      info.setElement(StoreInfo.ADDRESS, ddressfield.getText());
      info.setElement(StoreInfo.PHONE, phonefield.getText());
      info.setElement(StoreInfo.SPECIALTY, specialtyfield.getText());
      _data.addElement(info);
      synchronized (store) {
        store.setContents(_data);
        store.commit();
      }
      Dialog.inform("Success!");
      namefield.setText(null);
      addressfield.setText(null);
      phonefield.setText("");
      specialtyfield.setText("");
    }
  }; 

  private MenuItem getItem = new MenuItem("Get", 110, 11) {
    public void run() {
      synchronized (store) {
        _data = (Vector) store.getContents();
        if (!_data.isEmpty()) {
          StoreInfo info = (StoreInfo) _data.lastElement(); 

          namefield.setText(info.getElement(StoreInfo.NAME));
          addressfield.setText(info.getElement(StoreInfo.ADDRESS));
          phonefield.setText(info.getElement(StoreInfo.PHONE));
          specialtyfield.setText(info.getElement(StoreInfo.SPECIALTY));
        }
      }
    }
  }; 

  static {
    store = PersistentStore.getPersistentObject(0xdec6a67096f833cL);
    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 ADDRESS = 1;
    public static final int PHONE = 2;
    public static final int SPECIALTY = 3; 

    public StoreInfo() {
      _elements = new Vector(4);
      for (int i = 0; i < _elements.capacity(); ++i) {
        _elements.addElement(new String(""));
      }
    } 

    public String getElement(int id) {
      return (String) _elements.elementAt(id);
    } 

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

I put this code into two parts, the first part to record the second part to get info and the info, and they use the same class of StoreInfo, here is the code of the first part.

import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.system.*;
import net.rim.device.api.util.*;
import java.util.*; 

public class SaveMusicInfo extends UiApplication { 

  private AutoTextEditField namefield;
  private AutoTextEditField addressfield;
  private EditField phonefield;
  private EditField specialtyfield;
  private static Vector _data;
  private static PersistentObject store; 

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

  public SaveMusicInfo() {
    MainScreen mainScreen = new MainScreen();
    mainScreen.setTitle(new LabelField("Favourite Music Store"));
    namefield = new AutoTextEditField("Store Name: ", "");
    addressfield = new AutoTextEditField("Address: ", "");
    phonefield = new EditField("Phone Number: ", "", Integer.MAX_VALUE, BasicEditField.FILTER_PHONE);
    specialtyfield = new EditField("Music Type: ", "", Integer.MAX_VALUE, BasicEditField.FILTER_DEFAULT);
    mainScreen.add(namefield);
    mainScreen.add(addressfield);
    mainScreen.add(phonefield);
    mainScreen.add(specialtyfield);
    mainScreen.addMenuItem(saveItem);
    mainScreen.addMenuItem(getItem);
    pushScreen(mainScreen);
  } 

  private MenuItem saveItem = new MenuItem("Save", 110, 10) {
    public void run() {
      StoreInfo info = new StoreInfo();
      info.setElement(StoreInfo.NAME, namefield.getText());
      info.setElement(StoreInfo.ADDRESS, ddressfield.getText());
      info.setElement(StoreInfo.PHONE, phonefield.getText());
      info.setElement(StoreInfo.SPECIALTY, specialtyfield.getText());
      _data.addElement(info);
      synchronized (store) {
        store.setContents(_data);
        store.commit();
      }
      Dialog.inform("Success!");
      namefield.setText(null);
      addressfield.setText(null);
      phonefield.setText("");
      specialtyfield.setText("");
    }
  }; 

  static {
    store = PersistentStore.getPersistentObject(0xdec6a67096f833cL);
    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 ADDRESS = 1;
    public static final int PHONE = 2;
    public static final int SPECIALTY = 3; 

    public StoreInfo() {
      _elements = new Vector(4);
      for (int i = 0; i < _elements.capacity(); ++i) {
        _elements.addElement(new String(""));
      }
    } 

    public String getElement(int id) {
      return (String) _elements.elementAt(id);
    } 

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

Following is the code of the second part.

import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.system.*;
import net.rim.device.api.util.*;
import java.util.*; 

public class GetMusicInfo extends UiApplication { 

  private AutoTextEditField namefield;
  private AutoTextEditField addressfield;
  private EditField phonefield;
  private EditField specialtyfield;
  private static Vector _data;
  private static PersistentObject store; 

  public static void main(String[] args) {
    GetMusicInfo app = new GetMusicinfo();
    app.enterEventDispatcher();
  } 

  public GetMusicInfo() {
    MainScreen mainScreen = new MainScreen();
    mainScreen.setTitle(new LabelField("Favourite Music Store"));
    namefield = new AutoTextEditField("Store Name: ", "");
    addressfield = new AutoTextEditField("Address: ", "");
    phonefield = new EditField("Phone Number: ", "", Integer.MAX_VALUE, BasicEditField.FILTER_PHONE);
    specialtyfield = new EditField("Music Type: ", "", Integer.MAX_VALUE, BasicEditField.FILTER_DEFAULT);
    mainScreen.add(namefield);
    mainScreen.add(addressfield);
    mainScreen.add(phonefield);
    mainScreen.add(specialtyfield);
    mainScreen.addMenuItem(saveItem);
    mainScreen.addMenuItem(getItem);
    pushScreen(mainScreen);
  } 

  private MenuItem getItem = new MenuItem("Get", 110, 11) {
    public void run() {
      synchronized (store) {
        _data = (Vector) store.getContents();
        if (!_data.isEmpty()) {
          StoreInfo info = (StoreInfo) _data.lastElement(); 

          namefield.setText(info.getElement(StoreInfo.NAME));
          addressfield.setText(info.getElement(StoreInfo.ADDRESS));
          phonefield.setText(info.getElement(StoreInfo.PHONE));
          specialtyfield.setText(info.getElement(StoreInfo.SPECIALTY));
        }
      }
    }
  }; 

  static {
    store = PersistentStore.getPersistentObject(0xdec6a67096f833cL);
    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 ADDRESS = 1;
    public static final int PHONE = 2;
    public static final int SPECIALTY = 3; 

    public StoreInfo() {
      _elements = new Vector(4);
      for (int i = 0; i < _elements.capacity(); ++i) {
        _elements.addElement(new String(""));
      }
    } 

    public String getElement(int id) {
      return (String) _elements.elementAt(id);
    } 

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

I'm runing the Project One (first part) and record information to be stored, and then I WANT TO runing the project two (part two), but I received an error message telling me there are two same defined the class in the system, I try to change the class name of StoreInfo in two project, the last of error messge disappeared , instead of a CastClassException error, I am so sad...

For a long time debugging I found some interesting things, look at the below codes to two projects, please read the name of the StoreInfor class, not StoreInfo.

 private MenuItem getItem = new MenuItem("Get", 110, 11) {
    public void run() {
      synchronized (store) {
        _data = (Vector) store.getContents();
        if (!_data.isEmpty()) {
          StoreInfor info = (StoreInfor) _data.lastElement();
        }
      }
    }
  };

Now the _data class name is StoreInfo@xxxxxx (using _data.getClass () .getName ()) and class StoreInfo project two was renamed to StoreInfor, of course, I received a CastClassException error.

I finished the story, and I need some guys to help solve this problem, tks a lot! ~

Creating a descriptor application blackberry project and inside, choose library. Then, each project must refer to this library.

Tags: BlackBerry Developers

Similar Questions

Maybe you are looking for

  • Importing the photos

    Hey, today I tried to import a few photos from my iPhone 6 (iOS 9.2) for my mbp (OS X 10.11.2).) When I have connected the device 2, via cable and opened Photos the photos I did today look like in tie (the previous photos were ok). And after a while

  • Iconia B1-720: will not update to OTA!

    Today, I got the update notice for my Tablet and it errors after restarting and tells me that the update failed! Any suggestions?

  • my laptop does not open my SG card, and he has done it before, what happens?

    SanDisk card. Laptop is vista windows 7, I think He opened a few days ago, and the photos are visible on the camera, but all suddenly not on the laptop. It says file not found.

  • Allowing APs clustering with an AP541N?

    I have an AP541N and need to extend the network wireless with a second wireless access point.  Can AP what models I use with the AP541N to achieve a 'cluster', please? Is it just with a second AP541N or can I use other Cisco access points to reach th

  • RV082 and Windows 7, no internet connection with active firewall

    Hello I have a 1.3.98 - tm firmware RV082 and have recently upgraded several Machines to glass & RC for testing, but they can't access the internet. They can access all the other mahcines internal, but cannot ping the router.  The first Pc I upgraded