I am trying to store the registration state of the user in my app. i have used Persistent store. But i am not getting the data bak from the store. I have written the below code:
package com.saiservices.util;
import java.util.Enumeration;
import com.saiservices.ui.Logger;
import net.rim.device.api.system.PersistentObject;
import net.rim.device.api.system.PersistentStore;
import net.rim.device.api.util.Persistable;
public class SaiPersistentStore implements Persistable{
private SaiHashTable mElements = null;
static SaiPersistentStore instance;
private static final long KEY = xxxxxxxxxxxx;
private PersistentObject persistentObject = null;
/**
* Gets the single instance of DataBase.
*
* @return single instance of DataBase
*/
public static SaiPersistentStore getInstance() {
try {
if (instance == null) {
instance = new SaiPersistentStore();
}
} catch (Exception e) {
instance = null;
}
return instance;
}
private SaiPersistentStore()
{
Logger.out("PersistentStoreInfo", "inside constructor ");
persistentObject = PersistentStore.getPersistentObject(KEY);
synchronized (persistentObject)
{
if (persistentObject.getContents() == null)
{
persistentObject.setContents(new SaiHashTable());
persistentObject.commit();
}
}
mElements = (SaiHashTable)persistentObject.getContents();
if (mElements == null){
Logger.out("PersistentStoreInfo", "************"+(mElements == null));
mElements = new SaiHashTable();
}
}
public String getElement(String key)
{
Logger.out("PersistentStoreInfo", "getElement :"+key);
Logger.out("PersistentStoreInfo", "getElement value :"+mElements.get(key));
return (String) mElements.get(key);
}
public void setElement(String key, String value)
{
Logger.out("PersistentStoreInfo", "setElement11111 :"+key + " "+value);
mElements.put(key, value);
persistentObject.setContents(mElements);
Logger.out("PersistentStoreInfo", "setElement22222 :"+key+" "+value);
persistentObject.commit();
}
public Enumeration getAllKeys()
{
return mElements.keys();
}
}
And here is the hashtable code:
package com.saiservices.util;
import java.util.Hashtable;
import net.rim.device.api.util.Persistable;
public class SaiHashTable extends Hashtable implements Persistable{
public SaiHashTable(){
super();
}
public SaiHashTable(int initialCepacity){
super(initialCepacity);
}
}
Now i am setting the element like this:
SaiPersistentStore.getInstance().setElement("Registration","on");
But when i am trying to get the element in theis way, gettting null value:
SaiPersistentStore.getInstance().getElement("Register")
Where i am doing wrong here? Please help.