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
New Contributor
prime97
Posts: 9
Registered: ‎03-20-2013
My Device: Torch 9800
Accepted Solution

After app update, cannot read persistent store on some devices

Hi,

 

I have a problem in an app which I've been able to reproduce in a simple test app.

The test app writes some info to the persistent store, then can read it back again and it seems to work fine.

 

If I change the version number of the app, then download the new version (by downloading a new jad file in Browser), it prompts me to replace the old version w/ the new version, which I confirm. After downloading the new version of the app, it prompts me to reboot the device, which I do.

 

On most devices that I've tested this on,  after reboot, when I run the updated app, it works as expected - it can read the value that was written to the persistent store by the prior version of the app.

 

However on some devices it is unable to do so. 

Debugging a little, the error occurs when trying to get the persistent object via:

 

PersistentObject po1 = PersistentStore.getPersistentObject(MY_KEY);

 That line throws an exception - the exception message is simply "null". I don't know specifically what the type of the exception is since this is a static method not expected to throw any exception as far as I'm aware, so I'm just catching a generic java.lang.Exception.

 

Similarly - in the updated version of the app, if the software tries to write to the persistent store using the same object ID, the result is the same - the call to getPersistentObject throws a "null" exception.

 

Note that I'm not changing anything about the object that's being saved to the persistent store, and I'm not changing the object identifier between versions of the app.

 

A device on which I can consistently reproduce this issue is a Blackberry 9800 (Torch) running 6.0 Bundle 2647.

 

Any advice or suggestions how to debug this appreciated.

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

Re: After app update, cannot read persistent store on some devices

Not seen this on anything I have tested.

 

Can you replicate on a Simulator?

New Contributor
prime97
Posts: 9
Registered: ‎03-20-2013
My Device: Torch 9800

Re: After app update, cannot read persistent store on some devices

Unfortunately I can't reproduce this on a simulator, at least not so far.
Developer
peter_strange
Posts: 19,609
Registered: ‎07-14-2008
My Device: Not Specified

Re: After app update, cannot read persistent store on some devices

Shame, because I guess most people will struggle to get a matching device to test on. But obviously a Simulator is easy.

 

My only suggestion is to create a sample sample app that demos the problem, and put it on issue tracker.  You might like to put it here too and people can test it on their devices.   

New Contributor
prime97
Posts: 9
Registered: ‎03-20-2013
My Device: Torch 9800

Re: After app update, cannot read persistent store on some devices

Thanks Peter. 

I will have to spend more time trying on the simulator - I'm not sure if the sim I'm using is the exact same version of the OS as what's on the device.

 

I'm happy to post my sample code here - what is the right way to do that, just paste all the source code?

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

Re: After app update, cannot read persistent store on some devices

Possibly the best way is to create a zip file of the sample project, and then attach it.  But to see the attachments option you have to be recognised as a real developer, and to do that, you need to add your name to this list:

http://supportforums.blackberry.com/t5/Java-Development/Developers-Start-here/td-p/1459329

and wait for one of the mods to sanction you. 

 

In the meantime, if you have access to some web site, you can put the zip fie there, then we can use the URL to download it.  People use file sharing sites, but I (for example) will never create a log in to get things like this.

 

But if there are relatively few lines, you can also post in here.  Note the "Insert Code" button, it will open a window to paste code into.  Do that for each class. 

New Contributor
prime97
Posts: 9
Registered: ‎03-20-2013
My Device: Torch 9800

Re: After app update, cannot read persistent store on some devices

Well the code is pretty small so I'll go ahead & paste it. There are three files.

 

This is the main class - straight from HelloBlackberry. It loads the HelloBlackBerryScreen.

File: HelloBlackberry.java

package mypackage;

import net.rim.device.api.ui.UiApplication;

/**
 * This class extends the UiApplication class, providing a graphical user interface.
 */
public class HelloBlackBerry extends UiApplication {
    /**
     * Entry point for application
     * 
     * @param args
     *            Command line arguments (not used)
     */
    public static void main( String[] args ) {
        // Create a new instance of the application and make the currently
        // running thread the application's event dispatch thread.
        HelloBlackBerry theApp = new HelloBlackBerry();
        theApp.enterEventDispatcher();
    }

    /**
     * Creates a new HelloBlackBerry object
     */
    public HelloBlackBerry() {
        // Push a screen onto the UI stack for rendering.
        pushScreen( new HelloBlackBerryScreen() );
    }
}

 

The 2nd file declares a class named SessionInfo, which is a simple wrapper around a string and implements Persistable so that it can be stored in the persistent store.

FIle: SessionInfo.java

package mypackage;

import net.rim.device.api.util.Persistable;

public class SessionInfo  implements Persistable {
    public String session;
    public SessionInfo() {
    	
    }
}

 The 3rd & last file defines the HelloBlackberryScreen class. Just puts a text field and buttons to allow reading & writing  the persistent store.  I marked in bold the lines that throw an exception when running on the Torch after app update.

 

File: HelloBlackberryScreen.java:

package mypackage;

import net.rim.device.api.system.PersistentObject;
import net.rim.device.api.system.PersistentStore;
import net.rim.device.api.system.ControlledAccessException;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.component.BasicEditField;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.Menu;
import net.rim.device.api.util.StringProvider;

public class HelloBlackBerryScreen extends MainScreen {
	private BasicEditField basicEditField;

	public static final long NEWMAN_KEY = 0xa4bf7dc3538433bdL;
	public HelloBlackBerryScreen() {
		super( MainScreen.VERTICAL_SCROLL | MainScreen.VERTICAL_SCROLLBAR );
		setTitle( "HelloBlackBerry" );

		basicEditField = new BasicEditField( "Enter Name:", "", 100, BasicEditField.EDITABLE );
		add( basicEditField );

		ButtonField bf3 = new ButtonField("Read Store",ButtonField.CONSUME_CLICK | ButtonField.FIELD_RIGHT );
		add ( bf3 );
		bf3.setChangeListener( new FieldChangeListener() {
			public void fieldChanged( Field arg0, int arg1 ) {
				readStore();
			}
		} );

		ButtonField bf4 = new ButtonField("Write Store",ButtonField.CONSUME_CLICK | ButtonField.FIELD_RIGHT );
		add ( bf4 );
		bf4.setChangeListener( new FieldChangeListener() {
			public void fieldChanged( Field arg0, int arg1 ) {
				writeStore();
			}
		} );

	}

	private void readStore() {
		try {
			//  Try to read value from the persistent store so we can see if it fails after update
			Dialog.inform("readStore IN");
			PersistentObject po1 = PersistentStore.getPersistentObject(NEWMAN_KEY);
			Dialog.inform("readStore got p01, checking it for null.");
			if (po1==null) {
				Dialog.inform("Can't read val from store - po1 null");
				return;
			}
			Dialog.inform("readStore calliung getContents.");
			Object anObj = po1.getContents();
			Dialog.inform("Checking anObj for null.");
			if (anObj == null) {
				Dialog.inform("Can't read val from store - anObj null");
				return;
			}
			Dialog.inform("readStore going to cast anObj");
			SessionInfo sess = (SessionInfo)anObj;
			Dialog.inform("readStore cast complete.");
			if (sess==null) {
				Dialog.inform("Readstore sess null !?!");
				return;
			}
			Dialog.inform("readStore checking sess.session");
			if (sess.session==null) {
				Dialog.inform("Readstore sess.session null !!!");
				return;
			}
			Dialog.inform("Read from store: " + sess.session);
		} catch (Exception ee) {
			Dialog.inform("readStore exception: " + ee.getMessage());
		}
	}
	
	private void writeStore() {
		try {
			Dialog.inform("writeStore IN");
			// Write the value from the edit field into the persistent store
			PersistentObject po2 = PersistentStore.getPersistentObject(NEWMAN_KEY); 
			Dialog.inform("WriteStore got po.");
			if (po2==null) {
				Dialog.inform("Can't write val to store - po2 null");
				return;
			}
			Dialog.inform("WriteStore creating new SessionInfo");
			SessionInfo moo = new SessionInfo();
			Dialog.inform("Writestore checking bef null");
			if (basicEditField==null) {
				Dialog.inform("writeStore and basicEditField is null!");
				return;
			}
			Dialog.inform("WriteStore calling bef.getText()");
			String theText = basicEditField.getText();
			if (theText==null) {
				moo.session="Default Text";
			} else {
				moo.session=theText;
			}
			Dialog.inform("WriteStore going to call setContents.");
			try {
				po2.setContents(moo);
				po2.commit();
				Dialog.inform("Sucess - wrote value to store: " + moo.session);
			} catch ( ControlledAccessException cae1) {
				Dialog.inform("Can't write val to store - CAE");
				return;
			} catch (Exception e12) {
				Dialog.inform("Can't write val to store: " + e12.getMessage());
				return;
			}
		} catch (Exception ee2) {
			Dialog.inform("writeStore exception: " + ee2.getMessage());
		}
	}
}

 

There are a bunch of Dialog.inform statements in the code that I use as a debugging tool.

Again as a reminder, this works fine on most devices, including several running 7.0 that I tried. But if I run this on my Torch 9800 running 6.0, it throws an exception.

 

Thanks!

BlackBerry Development Advisor
MSohm
Posts: 14,752
Registered: ‎07-09-2008
My Device: BlackBerry Passport

Re: After app update, cannot read persistent store on some devices

I tried to reproduce this on a Blackberry 9800 (Torch) running 6.0 Bundle 2647, same as what you report above but found the sample code you posted worked fine.  Were there any other changes between versions other than incrementing the version number?

 

Is this happening on multiple devices or just a single one?  If it's just one device you may want to try reloading the device software in case something has become corrupt.

Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
New Contributor
prime97
Posts: 9
Registered: ‎03-20-2013
My Device: Torch 9800

Re: After app update, cannot read persistent store on some devices

Mark,

I changed the software version on the Torch to 6.0 bundle 278 using the Blackberry Desktop Software. After I did this, I could no longer reproduce the problem, which is encouraging.

 

Now I'd like to re-load bundle 2647 and re-test, but I'm not sure how to do so. The Desktop software says there are no updates available, and when I try OTA update is says it cannot send the request to update.  

 

Do you know how I can downoad & install bundle 2647 again?

 

Thanks,

Scott

BlackBerry Development Advisor
MSohm
Posts: 14,752
Registered: ‎07-09-2008
My Device: BlackBerry Passport

Re: After app update, cannot read persistent store on some devices

You may not see updates unless you have an active sim card in the device.  Is there one in it?

Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker