0

I have the following code:

public class CustomGesture extends Activity {

private static Spinner spinner1, spinner2;
private static Button btn;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.custom_gesture);
    spinner1 = (Spinner) findViewById(R.id.spinner1);
    spinner2 = (Spinner) findViewById(R.id.spinner2);
    // list.add("peepu");
    // list.add("Cheecki");
    List<String> list = new ArrayList<String>();
    list.add("cheeku");
    list.add("matthu");
    SharedPreferences appSharedPrefs = PreferenceManager
            .getDefaultSharedPreferences(this.getApplicationContext());
    Editor prefsEditor = appSharedPrefs.edit();
    Gson gson = new Gson();
    String json = gson.toJson(list);
    prefsEditor.putString("MyObject", json);
    prefsEditor.commit();
    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, list);
    dataAdapter
            .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner1.setAdapter(dataAdapter);
    btn = (Button) findViewById(R.id.btnSubmit);
    btn.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                     SharedPreferences appSharedPrefs = PreferenceManager
                              .getDefaultSharedPreferences(getApplicationContext());
                              Gson gson = new Gson();
                              String json = appSharedPrefs.getString("MyObject", "");
                              List<String> list = gson.fromJson(json, null);
                    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, list);
                    dataAdapter
                            .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                    spinner2.setAdapter(dataAdapter);
                }
            }).start();
        }
    });
}

}

the error I'm getting is the following :

04-13 13:50:20.066: E/AndroidRuntime(2358): FATAL EXCEPTION: Thread-5592
04-13 13:50:20.066: E/AndroidRuntime(2358): java.lang.NullPointerException
04-13 13:50:20.066: E/AndroidRuntime(2358):     at com.google.gson.internal.$Gson$Preconditions.checkNotNull($Gson$Preconditions.java:35)
04-13 13:50:20.066: E/AndroidRuntime(2358):     at com.google.gson.reflect.TypeToken.<init>(TypeToken.java:72)
04-13 13:50:20.066: E/AndroidRuntime(2358):     at com.google.gson.reflect.TypeToken.get(TypeToken.java:296)
04-13 13:50:20.066: E/AndroidRuntime(2358):     at com.google.gson.Gson.fromJson(Gson.java:801)
04-13 13:50:20.066: E/AndroidRuntime(2358):     at com.google.gson.Gson.fromJson(Gson.java:768)
04-13 13:50:20.066: E/AndroidRuntime(2358):     at com.google.gson.Gson.fromJson(Gson.java:717)
04-13 13:50:20.066: E/AndroidRuntime(2358):     at com.google.gson.Gson.fromJson(Gson.java:689)
04-13 13:50:20.066: E/AndroidRuntime(2358):     at com.winacro.Bsquares.CustomGesture$1$1.run(CustomGesture.java:60)
04-13 13:50:20.066: E/AndroidRuntime(2358):     at java.lang.Thread.run(Thread.java:841)

I think this error is because Im using null as a parameter in the fromJSON ,but what to use instead of the null parameter I think I cannot use a list there?

2
  • ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, list); Commented Apr 13, 2014 at 9:04
  • OKay I solved it I need to make the Array that I'm feeding to the spinner compatible with the list type. Commented Apr 13, 2014 at 14:39

1 Answer 1

1

SharedPrefrences is used to store small amount of data, such as flags and static strings, you went for a workaround to store a json-formatted string it works for a very small json object otherwise you need to use sqlite, here is a tutoial

Sign up to request clarification or add additional context in comments.

1 Comment

I'm trying to add the Data as >> db.addDATA(new Data("Srinivas", "9199999999")); but I'm getting it saved as04-13 17:48:33.246: D/CHECKKK********(22741): com.example.cheeki.Data@429af408 when I retrieve it!

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.