android - Checking whether a list item is there in ssharedpreference -
in app use ssharedpreference save list items when clicked on button , retrieving these items activity. in other activity want check if list item present in ssharedpreference or not ,so created method check. if run app, crashes showing nullpointerexception in logcat.
method check ssharedpreference
codelist codes = (codelist) getrritem(position); private boolean checkarchiveditem(codelist checkcodes) { boolean check = false; list<codelist> archives = archvprefrnces.getarchives(interactivity.this); if (archives != null) { (codelist codes : archives) { if (codes.equals(checkcodes)) { check = true; break; } } } return check; }
this line being mentioned in logcat
list<codelist> archives = archvprefrnces.getarchives(interactivity.this);
my sharedpreference
public class archivepreferences { public static final string prefs_name = "code_archives"; public static final string archived = "code_archived"; public archivepreferences(){ super(); } public void savearchives(context context, list<codelist> archives){ sharedpreferences settings; editor editor; settings = context.getsharedpreferences(prefs_name, context.mode_private); editor = settings.edit(); gson gson = new gson(); string jsonarchives = gson.tojson(archives); editor.putstring(archived, jsonarchives); editor.commit(); } public void addarchive(context context, codelist code){ list<codelist> archives = getarchives(context); if(archives == null) archives = new arraylist<codelist>(); archives.add(code); savearchives(context,archives); } public void removearchive(context context, codelist code) { arraylist<codelist> archives = getarchives(context); if (archives != null) { archives.remove(code); savearchives(context, archives); } } public arraylist<codelist> getarchives(context context) { sharedpreferences settings; list<codelist> archives; //arraylist<codelist> favorites; settings = context.getsharedpreferences(prefs_name, context.mode_private); if (settings.contains(archived)) { string jsonarchives = settings.getstring(archived, null); gson gson = new gson(); codelist[] archiveitems = gson.fromjson(jsonarchives, codelist[].class); archives = arrays.aslist(archiveitems); archives = new arraylist<codelist>(archives); } else return null; return (arraylist<codelist>) archives; //return favorites; } }
logcat
01-16 16:38:32.395 22165 22165 d androidruntime shutting down vm 01-16 16:38:32.402 22165 22165 e androidruntime fatal exception: main 01-16 16:38:32.402 22165 22165 e androidruntime process: com.enlightenme.pac, pid: 22165 01-16 16:38:32.402 22165 22165 e androidruntime java.lang.nullpointerexception: attempt invoke virtual method 'java.util.arraylist com.enlightenme.pac.archivepreferences.getarchives(android.content.context)' on null object reference 01-16 16:38:32.402 22165 22165 e androidruntime @ com.enlightenme.pac.interactivity$remotedatatask$100000001.checkarchiveditem(interactivity.java:323) 01-16 16:38:32.402 22165 22165 e androidruntime @ com.enlightenme.pac.interactivity$remotedatatask$100000001.onmenuitemclick(interactivity.java:301) 01-16 16:38:32.402 22165 22165 e androidruntime @ com.baoyz.swipemenulistview.swipemenulistview$100000000.onitemclick(swipemenulistview.java:78) 01-16 16:38:32.402 22165 22165 e androidruntime @ com.baoyz.swipemenulistview.swipemenuview.onclick(swipemenuview.java:85) 01-16 16:38:32.402 22165 22165 e androidruntime @ android.view.view.performclick(view.java:4780) 01-16 16:38:32.402 22165 22165 e androidruntime @ android.view.view$performclick.run(view.java:19866) 01-16 16:38:32.402 22165 22165 e androidruntime @ android.os.handler.handlecallback(handler.java:739) 01-16 16:38:32.402 22165 22165 e androidruntime @ android.os.handler.dispatchmessage(handler.java:95) 01-16 16:38:32.402 22165 22165 e androidruntime @ android.os.looper.loop(looper.java:135) 01-16 16:38:32.402 22165 22165 e androidruntime @ android.app.activitythread.main(activitythread.java:5254) 01-16 16:38:32.402 22165 22165 e androidruntime @ java.lang.reflect.method.invoke(native method) 01-16 16:38:32.402 22165 22165 e androidruntime @ java.lang.reflect.method.invoke(method.java:372) 01-16 16:38:32.402 22165 22165 e androidruntime @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:903) 01-16 16:38:32.402 22165 22165 e androidruntime @ com.android.internal.os.zygoteinit.main(zygoteinit.java:698)
could missed second part there?
archivepreferences archvprefrnces = new archivepreferences();
Comments
Post a Comment