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


Thank you for visiting the BlackBerry Support Community Forums.

BlackBerry will be closing the BlackBerry Support Community Forums Device Forums on April 1st (Developers, see below)

BlackBerry remains committed to providing excellent customer support to our customers. We are delighted to direct you to the CrackBerry Forums, a well-established and thorough support channel, for continued BlackBerry support. Please visit http://forums.crackberry.com or http://crackberry.com/ask. You can also continue to visit BlackBerry Support or the BlackBerry Knowledge Base for official support options available for your BlackBerry Smartphone.

"When we launched CrackBerry.com 10 years ago, we set out to make it a fun and useful destination where BlackBerry Smartphone owners could share their excitement and learn to unleash the full potential of their BlackBerry. A decade later, the CrackBerry community is as active and passionate as ever and I know our knowledgeable members and volunteers will be excited to welcome and assist more BlackBerry owners with their questions."

- Kevin Michaluk, Founder, CrackBerry.com

Developers, for more information about the BlackBerry Developer Community please review Join the Conversation on the BlackBerry Developer Community Forums found on Inside BlackBerry.


Reply
Developer
Posts: 374
Registered: ‎02-12-2011
My Device: Bold 9780
My Carrier: Wind Mobile
Accepted Solution

Persistent Store

I want to store my settings in memory using persistent store but I am getting a crash, first the settings class.

 

 

import java.util.Hashtable;
import net.rim.device.api.system.PersistentObject;
import net.rim.device.api.system.PersistentStore;

public final class Settings 
{
	private PersistentObject store;
	private Hashtable settingsItems;

	public Settings() {
	}

	public void inicializate(long appGUID) {
		store = PersistentStore.getPersistentObject(appGUID);
		Hashtable temp = (Hashtable) store.getContents();
		settingsItems = temp == null ? new Hashtable() : temp;
	}
	
	public void addItem(String itemName, Object itemValue) {
		settingsItems.put(itemName, itemValue);
	}

	public Hashtable getItems() {
		return settingsItems;
	}

	public Object getItem(String itemName) {
		return settingsItems.get(itemName);
	}

	public void persist() {
		synchronized (store) {
			store.setContents(settingsItems);
			store.commit();
		}
	}
}

 

 

Then when I try to store, it crashes

 

 

Locations []myLocations = new Locations[20];
			for (int index = 0; index < 20; index++)
			{
				myLocations[index] = new Locations();
				myLocations[index].Name = Integer.toString(index);
				mySettings.addItem("Location"+myLocations[index].Name, myLocations[index]);
			}

 Then I would retrieve settings on startup like so.

 

 

 

Locations []myLocations2 = new Locations[20];
for (int index = 0; index < 20; index++)
{
       myLocations2[index] = new Locations();
       myLocations2[index] = (Locations) mySettings.getItem("Location"+Integer.toString(index));
}

 

 

 

 

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Persistent Store

What sort of crash do you get and on what line?

Developer
Posts: 374
Registered: ‎02-12-2011
My Device: Bold 9780
My Carrier: Wind Mobile

Re: Persistent Store

JVM Error 104

Uncaught:
NullPointerException

 

Crash is on mySettings.addItem("Location"+myLocations[index].Name, myLocations[index]);

in the second chunk of code.

 

 

Highlighted
Regular Contributor
Posts: 64
Registered: ‎01-27-2011
My Device: Not Specified

Re: Persistent Store

Could you confirm that the code to initialize is being called before the code to store?

Developer
Posts: 374
Registered: ‎02-12-2011
My Device: Bold 9780
My Carrier: Wind Mobile

Re: Persistent Store

The code is above

//create
Locations []myLocations = new Locations[20];
for (int index = 0; index < 20; index++)
{
//initialize
myLocations[index] = new Locations();
myLocations[index].Name = Integer.toString(index);
//store
mySettings.addItem("Location"+myLocations[index].Name, myLocations[index]);
}
Developer
Posts: 1,452
Registered: ‎11-06-2009
My Device: Torch 9810
My Carrier: WiFi

Re: Persistent Store

So mySettings is null

 

Replace it with settingsItems and you are good to go

 

 



"When you become a level 3 developer, you learn to communicate over the ether. I'm told that level 5 developers are ascend into a higher level of existence beyond the physical realm." gord888 @ crackberry
Developer
Posts: 374
Registered: ‎02-12-2011
My Device: Bold 9780
My Carrier: Wind Mobile

Re: Persistent Store

[ Edited ]

Sorry, but the first code block is Settings.java

so settingsItems is declared inside the settings class so I cannot access it from Home.java.

What I did was create an instance of the settings class which accesses settingsItems (e.g. mySettings.addItem() calls settingItems.put())

So mySettings is not null I do not think. When I set breakpoint the settingsItems.put() is crashing it when I call mySettings.addItem()

 

I am still crashing.

Developer
Posts: 1,452
Registered: ‎11-06-2009
My Device: Torch 9810
My Carrier: WiFi

Re: Persistent Store

Ok, it was not quite clear that mySettings is initialized

 

So the issue is with settingsItems


Rewrite the inicializate(long appGUID) a bit, something like this

 

 

	public void inicializate(long appGUID) {
            store = PersistentStore.getPersistentObject(appGUID);
            settingsItems = (Hashtable) store.getContents();

            if (settingsItems == null) 
            {
                settingsItems = new Hashtable();
                store.setContents(settingsItems);
                store.commit();
            }

	}

 

 



"When you become a level 3 developer, you learn to communicate over the ether. I'm told that level 5 developers are ascend into a higher level of existence beyond the physical realm." gord888 @ crackberry
Developer
Posts: 374
Registered: ‎02-12-2011
My Device: Bold 9780
My Carrier: Wind Mobile

Re: Persistent Store

Sorry it seems I left that out,

public Settings mySettings = new Settings();

declared above my function before its used.

Your revised inicializate produces the same crash
Developer
Posts: 1,452
Registered: ‎11-06-2009
My Device: Torch 9810
My Carrier: WiFi

Re: Persistent Store

After

public Settings mySettings = new Settings();

 

Do you call

mySettings.inicializate()

?

 

Or even better, put inicializate() in Settings() constructor



"When you become a level 3 developer, you learn to communicate over the ether. I'm told that level 5 developers are ascend into a higher level of existence beyond the physical realm." gord888 @ crackberry