1

I've been starting to develop a pretty basic app but I cant use sharedpreference for some reason. The app crashes when it should be saved.

this is my code:

SharedPreferences sp=getSharedPreferences("details1",0);
    SharedPreferences.Editor editor=sp.edit();
    editor.putString("etName",etName.getText().toString());
    editor.putString("etLName",etLName.getText().toString());
    editor.putString("etCity",etCity.getText().toString());
    editor.putString("btnDateOfBirth",btnDateOfBirth.getText().toString());
    editor.putString("btnGender",btnGender.getText().toString());
    editor.commit();

I've declared on the variables needed. What might cause it to crash ?

GuyTsir
  • 21
  • 6
  • 1
    can you post stacktrace and some more code. – Nayan Srivastava Aug 16 '16 at 17:44
  • Stacktrace mate, stacktrace. It's not happen "for some reason". It has a reason. – Ugurcan Yildirim Aug 16 '16 at 17:46
  • java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference at il.co.appschool.hw_ch7.MainActivity.saveDetails(MainActivity.java:110) at il.co.appschool.hw_ch7.MainActivity$HandleAlertDialogListener.onClick(MainActivity.java:101) edit:it happens also when it isnt null btw – GuyTsir Aug 16 '16 at 17:58

2 Answers2

0

Have you tried adding the context to the getSharedPreference like

Context context = getActivity(); SharedPreferences sp=context.getSharedPreferences("details1",Context.MODE_PRIVATE);

You need to specify the context requesting the SharedPreference

0

I generally use this way to get the sharedPreferences of my application:

SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);

Also, you should provide the stack trace because the error could as well come from the getText methods you use for example? You say the variables are declared, but it's not enough. They have to point to an existing TextView or you get a null pointer exception.

Additional trick: you can chain the putX methods as they return the editor.

editor.putString("etName",etName.getText().toString())
    .putString("etLName",etLName.getText().toString())
    .putString("etCity",etCity.getText().toString())
    .putString("btnDateOfBirth",btnDateOfBirth.getText().toString())
    .putString("btnGender",btnGender.getText().toString())
    .commit();

EDIT : from the stacktrace it seems that etName is null. So you should ensure it points to an existing TextView. Typically:

etName = findViewById(R.id.et_text);

if your TextView (or EditText) has the id et_text in your XML layout.

Louis Durand
  • 187
  • 9
  • It says the row the error occurs in is on the first put string row. So it might be there? – GuyTsir Aug 16 '16 at 18:00
  • Ok I've seen your stacktrace you commented and as I said, the problem comes from the TextViews and has nothing to do with the shared preferences. Your variables must point to an existing TextView or it raises a null pointer exception. – Louis Durand Aug 16 '16 at 18:04
  • Thanks for the quick replys btw. I pretty new to android developing so sorry in advance. My EditText variables werent null when I tried to use sp. HUGE EDIT: I THINK U MIGHT BE RIGHT! LET ME CHECK AGAIN! – GuyTsir Aug 16 '16 at 18:06
  • Well, you must be using EditText but EditText extends TextView (inherits) so I was being more general. Replace `TextView` with `EditText` and you should get it! ;) – Louis Durand Aug 16 '16 at 18:13
  • Haha HUGE EDIT :') Ok I'll wait then. If it does work, then mark my answer as accepted! – Louis Durand Aug 16 '16 at 18:17
  • 1
    IT WORKED! Thanks you so much for spending time on this! Such a beginner mistake on my part, didnt do the findviewbyid thing for the first name .. THANK YOU! – GuyTsir Aug 16 '16 at 18:20