0

I am building a camera application. I'm trying to save some information using SharedPreferences. I want to save some persistant information-the last image taken filepath. But the first time the application is used before taking a picture, the data would be NULL.

So I want to getSharedPreferences in onCreate and check if the value is null. But as far as I know the only way to use getSharedPreferences is only if you have called put on the Editor before. Hence, I am getting a NULL pointer exception on the SharedPreferences object the first time.

How do you resolve this?

     //inside on Create()
      imageData = getSharedPreferences("ImageData",MODE_PRIVATE);

    SharedPreferences.Editor prefEditor = imageData.edit();

    prefEditor.commit();
    String previousImage = imageData.getString("lastImageTaken", null);
    if(previousImage == null){

        Log.d(TAG,"previous image is NULL");
    }
    else{
        //do something with the filepath            

        }
  //-----------------------
//in onClick of capture button
imageData = getSharedPreferences("ImageData",MODE_PRIVATE);

            SharedPreferences.Editor prefEditor = imageData.edit();
            prefEditor.putString("lastImageTaken", MyActivity.this.pictureFilePath);
            prefEditor.commit();

9
  • can u shed some light on String previousImage, is it in shared prefs? Dec 13, 2011 at 5:14
  • What is use of SharedPreferences.Editor prefEditor = imageData.edit(); prefEditor.commit(); those llines? Dec 13, 2011 at 5:20
  • Your code shouldn't generate a null pointer exception, even if it does, it has nothing to do with the SharedPreference's non-existence. Post your stacktrace, maybe we can find something there. Dec 13, 2011 at 5:33
  • @Pankaj: The SharedPreferences object does not have any memory allocated to it. And we can't say new SharedPreferences. It says in the documentation that if the first argument to getSharedPreferences does not exist then the edit() call will create it. That's why I tried to put those 2 lines inthe hope of allocating memory to it. Please correct me if I'm wrong.
    – Namratha
    Dec 13, 2011 at 5:56
  • 1
    @Namratha: in oncreate obviously it will be null because u r trying to retrieve something that was never put Dec 13, 2011 at 6:47

1 Answer 1

1

Please try this
To read from SharedPreferences

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
    String name = preferences.getString("name","default");
    String email = preferences.getString("email","default");

To save into SharedPreferences

Editor edit = preferences.edit();
                   edit.putString("name", "Roy");
                   edit.putString("email", "[email protected]");
                   edit.commit();
1
  • Sandy is correct, there is an attribute that will save a default value if it is currently null.
    – jfisk
    Dec 13, 2011 at 5:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.