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
mapleleafs90
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));
}

 

 

 

 

Please use plain text.
Developer
peter_strange
Posts: 19,603
Registered: ‎07-14-2008
My Device: Not Specified

Re: Persistent Store

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

Please use plain text.
Developer
mapleleafs90
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.

 

 

Please use plain text.
Regular Contributor
spardhan
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?

Please use plain text.
Developer
mapleleafs90
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]);
}
Please use plain text.
Developer
QuiteSimple
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
Please use plain text.
Developer
mapleleafs90
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.

Please use plain text.
Developer
QuiteSimple
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
Please use plain text.
Developer
mapleleafs90
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
Please use plain text.
Developer
QuiteSimple
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
Please use plain text.