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
Highlighted
New Developer
Posts: 54
Registered: ‎10-07-2008
My Device: Not Specified

Issue using PersistenceObject

Hi everyone!

 

I have an issue using the PersistenceObject, when I want to create my PersistenceObject the app returns me an error of NullPointer Exception

 

This is my code

static PersistentObject store; static{ String str = "UserPassword"; TravexHelper th = new TravexHelper(); store = PersistentStore.getPersistentObject(th.StringToLong(str)); }

 

I run it in my simulator and a device 7100, with the .cod signed respectly in order to use Persistence, RollOverIcon and other functions.

 

Any help will be very appreciated.

 

Thanks in advance! Smiley Happy


 

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

Re: Issue using PersistenceObject

I recommend that you review the documentation and the sample code supplied in the Developer Guide - look for a chapter called "Storing Data".  Without knowing what StringToLong(..) does, I'm not sure that I can comment on your code snippet, also you don't tell us where the Null Pointer Exception is thrown.

 

You can effectively test Persistent Store using the Simulator, so I would not worry about trying your code on the device until you are sure it works OK on the Simulator.

New Developer
Posts: 54
Registered: ‎10-07-2008
My Device: Not Specified

Re: Issue using PersistenceObject

[ Edited ]

Thanks Peter,

 

The exception is thrown when I assign the value to store, the StringToLong is this code:

 

 

/**
* StringToLong
* @param str
* @return long
*/
public long StringToLong(String str) {
long hashValLong = 0;

SHA1Digest sha1Digest = new SHA1Digest();

sha1Digest.update(str.getBytes());

byte[] hashValBytes = sha1Digest.getDigest();

for(int i = 0; i < 8; i++) {
hashValLong |= ((long)(hashValBytes[i]) & 0x0FF) << (8*i);
}

return hashValLong;
}

 

I think that PersistentStore is null, but in the example I don´t see any initialization, I think because is static.

 

 

I tryed directly with the long that is used in the code sample. I reviewd the Chapter2 of the BB Dev Guide, before writing this code. What I need to store is the same, user and password.

 

Regards!

 

Message Edited by ArthurKnight on 11-19-2008 06:57 PM
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Issue using PersistenceObject

You can set break points and do debugging in the static content.  It gets run once whenever your class is referenced, and what you should put in there are one-off initializations that you need to do.

 

In this case, all the static code is doing is finding the PersistentObject that is associated with the application.  I doubt it is null.  But the contents of this are another question.....

 

In the user name and password example from the Developer Guide, we have the following:

store = PersistentStore.getPersistentObject(0xa1a569278238dad2L);

 

Note the ID 0xa1a569278238dad2L.  Call this your persistent "slot" for you application. Normally this is fixed for the life of your application, so your application always uses the same ID, even though the contents change.  What you seem to be doing in your code is altering the slot depending on the data.  I don't think this is a good idea.  If you do this, as your data changes, the slot you use will wonder around sticking bits of data in all sorts of places.  I actually don't think it is what you want anyway - you want to recover the User name and password from a known place. 

 

Note that the static code does not recover the username and password, to do that you need to do the following (also cut from the Developer Guide):.

String[] currentinfo = (String[])store.getContents();

This will return null if no data has been put in there,

 

When you update the contents, or put an object in there for the first time, you need to store it back:

synchronized(store) {
    store.setContents(userinfo);
    store.commit();
}

 

I hope this has explained a bit more about how to use Persistent Store, so that you can find your problem yourself.

New Developer
Posts: 54
Registered: ‎10-07-2008
My Device: Not Specified

Re: Issue using PersistenceObject

Hi Peter,

 

I cannot debug, I have a problem, the post# 9292. I was debugging then I cannot, I looked for a solution but still no solution anyway.

 

By debugging by alerts, I know that the store is not null. i´m using the same long value as the guide states.

 

This is the code, this is what I do to verify if prevously logged:

 

 

String[] currentinfo; store = PersistentStore.getPersistentObject(0xa1a569278238dad2L); synchronized(store) { currentinfo = (String[])store.getContents(); } if(currentinfo == null) { currentinfo = new String[]{user, password}; synchronized(store){ store.setContents(currentinfo); store.commit(); } } else{ if(currentinfo.length == 2){ user = (currentinfo[0].toString()); password = (currentinfo[1].toString()); validated = true; } }

I updated my code with your advices and I still getting the nullpointer exception.

 

I have to initialize the contents if is null right?? 

 

Thanks for your help!

 

 

Regards!

 

Developer
Posts: 1,415
Registered: ‎07-30-2008
My Device: Not Specified

Re: Issue using PersistenceObject

I've used stuff like this before ( names changed ) with good luck,

 

static final long pik_hist=StringUtilities.stringHashToLong("com.lehman.mbs.history");
 static final PersistentObject persist_hist = PersistentStore.getPersistentObject( pik_hist );

and then you can use setcontent and commit as shown.

 

Note that you can get some wierd things, at least on real devices, when you update the app and maybe

when you don't commit etc. I'd suggest uninstalling and then re-installing if you see odd things in a real phone.

 

Also, as a rule in this environment, catch the exception along with as much unambigious context

as possible. If you can't programmatically integrate a stack trace on a real device you have to be careful.

NPE's are pretty common mistakes, so don't assume " I tried something fancy and got an NPE" means that

"something fancy" caused the NPE.

 

 

 

 

 

 

New Developer
Posts: 54
Registered: ‎10-07-2008
My Device: Not Specified

Re: Issue using PersistenceObject


marchywka wrote:

I've used stuff like this before ( names changed ) with good luck,

 

static final long pik_hist=StringUtilities.stringHashToLong("com.lehman.mbs.history");
 static final PersistentObject persist_hist = PersistentStore.getPersistentObject( pik_hist );

and then you can use setcontent and commit as shown.

 

Note that you can get some wierd things, at least on real devices, when you update the app and maybe

when you don't commit etc. I'd suggest uninstalling and then re-installing if you see odd things in a real phone.

 

Also, as a rule in this environment, catch the exception along with as much unambigious context

as possible. If you can't programmatically integrate a stack trace on a real device you have to be careful.

NPE's are pretty common mistakes, so don't assume " I tried something fancy and got an NPE" means that

"something fancy" caused the NPE.

 

 


Hi marchywka,

 

I used a code to convert from string to long, but doesnt like it, so I used the value directly.

 

But I have a nullpointer error.

 

I´ll clean the simulator, and remove the app from my 7100, as you say, maybe there can be initialized with weird data.

 

I´ll let you know if it worked.

 

Regards!