Question

So from what I understand of the Preferences api, I can save a string/int/etc with a unique value as name with preferences.set(String, String).

But why do I get a Hastable nullpointer when I'm simply doing this:

private Preferences pref;
private String prefString, hold;
...

pref.set(prefString, hold);
Was it helpful?

Solution

private Preferences pref;
private String prefString, hold;

You have declared pref, but you have not initialized it. That is why

pref.set(prefString, hold);

throws a NullPointerException, because null has no function called set. Null has nothing. Null is nothing.

Normally, I'd say you need to initialize pref with something like

pref = new Preferences();

However, according to the codenameone API for Preferences, all it's functions are static, and should be called in this way:

Preferences.set(prefString, hold);

Therefore, its declaration line should not be there to begin with.


If

Preferences.set(prefString, hold);

still throws a NullPointerException, it is likely because 'prefString' is also null.

OTHER TIPS

Please make sure that Reference "pref" is initialised with an actual object. Generally null pointer occurs when you try to make a method call on an object that is null. If you ever see null pointer in a line, check the reference that is making call for instance:

String a = null
a.equals(someOtherString);

you know that a. is calling a method and it throws null pointer which means that a has to be null.

You are passing null to the key or the value where you are storing the data. Preferences doesn't accept null values. To remove a null value you need to explicitly delete it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top