sharred preference null pointer exception - android

//Activity-- mTweetApp.SetCaseInfo(type,teet); //storing in another class
public void SetCaseInfo(String PatientType,ArrayList arr){
// All objects are from
SharedPreferences settings =setSharedPreferences(DEALSPOTR_PREFS,0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("arrayLength",arr.size());
for(int i=0; i<=arr.size(); i++){
editor.putInt("Teethsselected"+String.valueOf(i), (Integer) arr.get(i));
}
editor.putString("view", PatientType);
for(int i=0; i<=arr.size(); i++){
System.out.println("Teethsselected-----"+(Integer) arr.get(i)+"type--->"+PatientType);
}
}
public void getCaseInfo() {
SharedPreferences settings =getSharedPreferences(DEALSPOTR_PREFS, 0);
int arraySize = settings.getInt("arrayLength", 0);
int teeth[] =new int[arraySize];
for(int i=0; i<arraySize; i++){
teeth[i] = ettings.getInt("Teethsselected"+String.valueOf(i),0);
}
String type =settings.getString("view"," ");
}
how to fix this?

You don't commit your changes. put editor.commit(); into your set method.

First, when you write your pref values, no call of commit().
Second, the pref file is not written to /data/data/com.package.App/shared_prefs/pref_file.xml, hence when reading the SharedPreferences object is NULL.

SharedPreferences.Editor modifications should be commited.
//Your code
editor.putString("view", PatientType);
for(int i=0; i<=arr.size(); i++){
System.out.println("Teethsselected-----"+(Integer) arr.get(i)+"type--->"+PatientType);
}
//ADD THIS
editor.commit();

SharedPreferences settings =setSharedPreferences(DEALSPOTR_PREFS,0);
instaed of above line, try using
SharedPreferences settings =getSharedPreferences(DEALSPOTR_PREFS,0);
and don't forget editor.commit() line once the values are added..

Related

SharedPreferences aren't resetting using remove(); or clear();

I'm trying to reset my SharedPreferences, but neither clear(), nor remove() work.
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
editor = preferences.edit();
for (int t = 0; t < homeTiles.size(); t++) {
editor.remove(homeTiles.get(t).getmFunction() + "_tile_pos"); //this isnt running
editor.remove(homeTiles.get(t).getmFunction() + "_tile_vis"); //this is getting ecexuted
}
editor.apply();
I also checked the preference file and it did'nt change.
try this
for (int t = 0; t < homeTiles.size(); t++) {
editor.remove(homeTiles.get(t).getmFunction() + "_tile_pos").remove(); //this isnt running
editor.remove(homeTiles.get(t).getmFunction() + "_tile_vis").remove(); //this is getting ecexuted
}

Sharedpreferences not saving data when app restart

I'm trying to make "add favorites" button in my app. I can send and get value with sharedpreferences properly, but when I restart the app, favorites are empty again. I guess I need override OnPause method, but I couldn't apply properly. I need help, thanks in advance.
PLAYING RADIO FRAGMENT
public static int incrementedValue = 0;
add_favorites_button= (Button) view.findViewById(R.id.add_favorites_button);
add_favorites_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
SharedPreferences settings = getActivity().getSharedPreferences("PREFS", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("radio_link"+ incrementedValue, radio_play_link);
editor.putString("radio_name" + incrementedValue, radio_name);
editor.putString("listener_number" + incrementedValue, listener_number);
//editor.clear();
editor.commit();
incrementedValue++;
}
});
FAVORITES FRAGMENT
final List<String> radio_name_list = new ArrayList<>();
final List<String> radio_link_list = new ArrayList<>();
final List<String> listener_numbers = new ArrayList<>();
for (int i=0; i<PlayRadioFragment.incrementedValue; i++) {
SharedPreferences settings = getActivity().getSharedPreferences("PREFS",0);
radio_name_list.add(settings.getString("radio_name" +i, ""));
radio_link_list.add(settings.getString("radio_link" +i, ""));
listener_numbers.add(settings.getString("listener_number" +i, ""));
}
...
then I show them in listview
...
for (int i=0; i<PlayRadioFragment.incrementedValue; i++)
After restarting the app (or Fragment), this incrementedValue will be back to what it was initialized with, probably 0, so you won't load anything from SharedPreferences (not entering the for-loop at all).
Try something like
for (int i=0; i<Integer.MAX_VALUE; i++) {
String s = settings.getString("radio_name" +i, "")
if (TextUtils.isEmpty(s))
break;
} else {
radio_name_list.add(s);
}
You probably also want to avoid overwriting the old favorites after the app restart, so you have to set the incrementedValue after loading the favorites like
incrementedValue = radio_name_list.size();
This is happening because on restarting app the variable named incrementedValue gets value once again as 0 and in for loop conditional statement is not full filling and that's why loop is not executing as expected. To resolve this issue you can do one more thing you need to save the value of incrementedValue in shared preference as well and in for loop testing statement get the updated value from shared preference. Hope that helps you

Shared Preferences not saving data

Initializing highScore array :
score = 0;
sharedPreferences = context.getSharedPreferences("Scores", Context.MODE_PRIVATE);
//initialize the array of high scores
highScore[0] = sharedPreferences.getInt("score1",0);
highScore[1] = sharedPreferences.getInt("score2",0);
highScore[2] = sharedPreferences.getInt("score3",0);
highScore[3] = sharedPreferences.getInt("score4",0);
highScore[4] = sharedPreferences.getInt("score5",0);
Checking For the 4 Highest Values :
highScore[5] = score;
Arrays.sort(highScore);
This is my code for saving data in shared preferences
SharedPreferences.Editor e = sharedPreferences.edit();
for(int j=4;j>=0;j--){
e.putInt("score"+(j+1),highScore[j]);
e.apply();
}
I will suggest to use like that.
SharedPreferences pref;
pref= context.getSharedPreferences("Scores", Context.MODE_PRIVATE);
SharedPreferences.Editor e = pref.edit();
for(int j=4;j>=0;j--){
e.putInt("score"+(j+1),highScore[i]);
}
e.apply();
Instead of commiting after completing the if loop , commit in each iteration of your loop like this:
SharedPreferences.Editor e = sharedPreferences.edit();
for(int j=4;j>=0;j--){
e.putInt("score"+(j+1),highScore[i]);
e.apply();
}
it will work.

Passing strings to list and deleting them using shared preferences

i wish to, in one activity, put strings in the sharedpreferences, so then, in another activity, i get those strings, put them in a array and display them sequentially. I managed to do this, but i don`t have any idea how can i delete one specified string when asked. These strings will just be scattered on the shared preferences,and i dont know how to keep track of them. I can pass this unique int id to each element. I tried to use LinkedList, but i cannot pass this kind of structure as a shared preferences. I did not managed to make Gson work also. Please help.
Method that gets the string and put on shared preferences:
public void makefavorites(String[] a, String[] b, int id)
{
int idfinal = id%10;
idfinal = idfinal+1;
a[idfinal] = b[idfinal] +"\n" + "\n"+ a[idfinal];
SharedPreferences prefs = getSharedPreferences("Favorites", Activity.MODE_PRIVATE);
Editor edit = prefs.edit();
int temp = prefs.getInt("favorites_size", 0);
edit.putInt("favorites_size", temp+1);
edit.putString("array_" + prefs.getInt("favorites_size", 0), a[idfinal]);
edit.commit();
refreshfavorites();
}
Method that gets those strings, put on array and display it:
public void refreshfavorites()
{
SharedPreferences prefs = getSharedPreferences("Favorites", Activity.MODE_PRIVATE);
//GETS THE ARRAY SIZE
int size = prefs.getInt("favorites_size", 0);
String[] array = new String[size];
for(int i=0; i<size; i++){
array[i] = prefs.getString("array_" + i, null);
}
}
you have to use editor.remove method to delete specific value from arraylist..
public void removeArray(String[] list)()
{
SharedPreferences.Editor editor = mSharedPrefs.edit();
int size = list.length();
for (int i = 0; i < size; i++) {
editor.remove("favorites_size"+i);
}
editor.commit();
}
i hope its useful to you..

How to store one string and arrayvalue in sharedpreference?

I have array values of up[]={0,0,0,0,0} and view="adult" thesetwo value i want to store and retrieve in from sharedpreference how to do that...
Assuming that you have a list of preferences for you app called MY_PREFS, I'd do this:
SharedPreferences settings = getSharedPreferences(MY_PREFS, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("arrayLength",up.size());
for(int i=0; i<up.size(); i++){
editor.putInt("up"+String.valueOf(i), up[i]);
}
editor.putString("view", "adult");
To retrieve them do:
SharedPreferences settings = getSharedPreferences(MY_PREFS, 0);
int arraySize = settings.getInt("arrayLength");
int up[] = new int[arraySize];
for(int i=0; i<arraySize; i++){
up[i] = settings.getInt("up"+String.valueOf(i));
}
String view = settings.getString("view");
you can do by simple way, like this
SharedPreferences settings = getSharedPreferences("pref_name", 0);
SharedPreferences.Editor editor = settings.edit();
String values="";
for(int i=0; i<yourArray.length; i++){
values+=","+Integer.toString(yourArray[i]);
}
editer.putString("array",values);
editor.putString("view", "adult");
To get those values,
SharedPreferences settings = getSharedPreferences("pref_name", 0);
String[] strArray=settings.getString("array").split(",");
int[] yourArray=new int[strArray.length];
for(int i=0;i<strArray.length;i++){
yourArray[i]=Integer.toParseInt(strArray[i]);
}
Well as far as I have seen you can't directly store an array in shared preferences. You can however use a for loop to save an int with an increasing name and have it call it back the same way when you need it and store it into another array.
for(int i=0; i<numInYourArray; i++){
editor.putInt("up"+i, up[i]);
}
If you aren't sure on how to use Shared Preferences in general look here

Categories

Resources