imattxc
imattxc

Reputation: 41

Android SDK SharedPreferences troubles

I'm working on an app and would like to save the state of obe of my checkbox options, but when I try using the SharedPreferences class (first time using it) I get a nullpointer exception. I have been troubleshooting for a couple hours and cant find a solution. Could anyone look at this and tell me what's wrong? The full code is alot longer but this is the park giving me the null pointer exception. I know for a fact it has to do with SharedPreferences.

package us.mattmccoy.meanfind;

//import java.util.Arrays;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ClipData;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {


//saved data stuff needed
SharedPreferences preferences = this.getSharedPreferences("us.mattmccoy.meanfind", Context.MODE_PRIVATE); 
Editor edit = preferences.edit();
//private data using saved data
String autoclearKEY = "us.mattmccoy.meanfind.AUTOCLEAR";
boolean autoclear = preferences.getBoolean(autoclearKEY, false);

//normal private data
final Context con1 = this;
int dividend;String dataFixed;boolean tb;
private CheckBox myCBox, acBox;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    addListenerBoxes();
    if(autoclear == true){
        acBox.setChecked(true);
    }else{
        acBox.setChecked(false);
    }
}
//check box listeners
public void addListenerBoxes(){
    //instantiate boxes
    acBox = (CheckBox) findViewById(R.id.chex2);
    acBox.setOnClickListener(new OnClickListener() {

          @Override
          public void onClick(View v) {
                    //is acBox checked?
            if (((CheckBox) v).isChecked()) {
                edit.putBoolean(autoclearKEY, true).commit();
            }else{
                edit.putBoolean(autoclearKEY, false).commit();
            }
          }
    });
    myCBox = (CheckBox) findViewById(R.id.chex);

}     

error message:

http://pastebin.com/5P2Mfwik

Upvotes: 0

Views: 259

Answers (1)

codeMagic
codeMagic

Reputation: 44571

Change this line

SharedPreferences preferences = this.getSharedPreferences("us.mattmccoy.meanfind", Context.MODE_PRIVATE); 

to

SharedPreferences preferences; = this.getSharedPreferences("us.mattmccoy.meanfind", Context.MODE_PRIVATE); 

and put

preferences = this.getSharedPreferences("us.mattmccoy.meanfind", Context.MODE_PRIVATE);

in onCreate()

You are trying to use Context before establishing it with onCreate() so you are getting NPE on context. You will also want to move these lines

Editor edit = preferences.edit();
boolean autoclear = preferences.getBoolean(autoclearKEY, false);

into onCreate() after you initialize preferences or you will get another NPE since preferences will be null until you initialize it. So it should be something like

public class MainActivity extends Activity {
//saved data stuff needed
SharedPreferences preferences;   //declare them
Editor edit;
//private data using saved data
String autoclearKEY = "us.mattmccoy.meanfind.AUTOCLEAR";
boolean autoclear;
        @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    SharedPreferences preferences; = this.getSharedPreferences("us.mattmccoy.meanfind",   Context.MODE_PRIVATE);
    Editor edit = preferences.edit();   //initialize them
    boolean autoclear = preferences.getBoolean(autoclearKEY, false);
    addListenerBoxes();
    if(autoclear == true){
        acBox.setChecked(true);
    }else{
        acBox.setChecked(false);
    }

If this doesn't solve your problem then please post full logcat

Upvotes: 1

Related Questions