Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
arindamhit
Posts: 271
Registered: ‎12-24-2010
My Device: BlackBerry 10
Accepted Solution

problem while getting the value from Persistent object

Hi,

 

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?

Developer
simon_hain
Posts: 16,282
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: problem while getting the value from Persistent object

are you using the code from different contexts (like autostart background process and normal process)?
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
arindamhit
Posts: 271
Registered: ‎12-24-2010
My Device: BlackBerry 10

Re: problem while getting the value from Persistent object

I am using in Normal process..

Developer
arindamhit
Posts: 271
Registered: ‎12-24-2010
My Device: BlackBerry 10

Re: problem while getting the value from Persistent object

Please help. I am blocked in this..

Developer
arindamhit
Posts: 271
Registered: ‎12-24-2010
My Device: BlackBerry 10

Re: problem while getting the value from Persistent object

Fixed it. Hashtable key was different. change it and it is working..