Jonathon Charles Loch Jonathon Charles Loch - 3 months ago 35
Android Question

Using SharedPreferences for custom ArrayList -- Adapter or Editor error?

I am attempting to save a custom ArrayList to SharedPreferences. Ideally the first time this app runs, there should be an empty SharedPreferences, but if I need to fill it with one item, then I can do that with an "Example."
I've tried using debug break points and have isolated the problem to the setAdapter line marked below.

The app crashes at the setAdapter line, but it may be due to the editor not being initialized? the debugger tells me "editor = null"

public class MainActivity extends AppCompatActivity {
ArrayList<PurchaseOrders> purchaseOrders = new ArrayList<PurchaseOrders>();
PurchaseOrderAdapter adapter;

public SharedPreferences sharedPref;
public SharedPreferences.Editor editor;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

purchaseOrders = new ArrayList<PurchaseOrders>();

String[] items = new String[]{"1", "2", "3"};

purchaseOrders.add(new PurchaseOrders(false, "purchaseNumber", "Customer", "Date 1", "Date 2", items));

save();

sharedPref = this.getSharedPreferences("com.lochdownstudios.simplePurchaseOrderer", Context.MODE_PRIVATE);
editor = sharedPref.edit();

load();

adapter = new PurchaseOrderAdapter(this, purchaseOrders);
final ListView listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(adapter); // Break Point here.
}

private void save() {
Gson gson = new Gson();
String json = gson.toJson(purchaseOrders);
editor.putString("savedPurchaseOrders", json); //editor = null .... hmmm....
editor.apply();
}

private void load() {
Gson gson = new Gson();
String json = sharedPref.getString("savedPurchaseOrders", "");
Type type = new TypeToken<List<PurchaseOrders>>(){}.getType();
purchaseOrders = gson.fromJson(json, type);
}
}


At the break point (mentioned in comments), my debugger tells me that "editor = null". And then the following step it crashes with the following logcat.

08-23 19:06:02.799 23974-23974/com.lochdownstudios.simplePurchaseOrderer E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.lochdownstudios.simplePurchaseOrderer, PID: 23974
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.lochdownstudios.simplePurchaseOrderer/com.lochdownstudios.simplePurchaseOrderer.MainActivity}: java.lang.NullPointerException: Attempt to invoke interface method 'android.content.SharedPreferences$Editor android.content.SharedPreferences$Editor.putString(java.lang.String, java.lang.String)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3253)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)
at android.app.ActivityThread.access$1100(ActivityThread.java:221)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7224)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'android.content.SharedPreferences$Editor android.content.SharedPreferences$Editor.putString(java.lang.String, java.lang.String)' on a null object reference
at com.lochdownstudios.simplePurchaseOrderer.MainActivity.save(MainActivity.java:119)
at com.lochdownstudios.simplePurchaseOrderer.MainActivity.onCreate(MainActivity.java:39)
at android.app.Activity.performCreate(Activity.java:6876)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3206)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)
at android.app.ActivityThread.access$1100(ActivityThread.java:221) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:158) 
at android.app.ActivityThread.main(ActivityThread.java:7224) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 


Thoughts? Thanks!

Answer

You are calling save which uses editor before you are actually initializing the editor

maybe change the order to

    sharedPref = this.getSharedPreferences("com.lochdownstudios.simplePurchaseOrderer", 
                                                                   Context.MODE_PRIVATE);
    editor = sharedPref.edit();

    save();