android - NullPointer while using sharedPreference in SplashScreen -


i working on application want store data in sharedpreference during splashscreen , read data once main activity loads up. if save data mainactivity data gets saved , able fetch back. if trying save data in splashscreen, nulpointer excpetion. here exception:

java.lang.runtimeexception: error occured while executing doinbackground()     @ android.os.asynctask$3.done(asynctask.java:299)     @ java.util.concurrent.futuretask$sync.innersetexception(futuretask.java:273)     @ java.util.concurrent.futuretask.setexception(futuretask.java:124)     @ java.util.concurrent.futuretask$sync.innerrun(futuretask.java:307)     @ java.util.concurrent.futuretask.run(futuretask.java:137)     @ android.os.asynctask$serialexecutor$1.run(asynctask.java:230)     @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1076)     @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:569)     @ java.lang.thread.run(thread.java:856)  caused by: java.lang.nullpointerexception     @ com.example.amandeepsingh.loginregisterandother.sharedpreferncesexecutor.save(sharedpreferncesexecutor.java:22)     @ com.example.amandeepsingh.loginregisterandother.splashscreenactivitywithasynctask$prefetchdata.doinbackground(splashscreenactivitywithasynctask.java:79)     @ com.example.amandeepsingh.loginregisterandother.splashscreenactivitywithasynctask$prefetchdata.doinbackground(splashscreenactivitywithasynctask.java:36)     @ android.os.asynctask$2.call(asynctask.java:287) 

here sample code:

splashscreenactivitywithasynctask:

public class splashscreenactivitywithasynctask extends activity{      //string now_playing, earned;     private sharedpreferncesexecutor sharedpreference;     activity context;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.splash_screen);          /**          * showing splashscreen while making network calls download necessary          * data before launching app use asynctask make http call          */         activity context = this;         new prefetchdata().execute();      }      /**      * async task make http call      */     private class prefetchdata extends asynctask<void, void, void> {          @override         protected void onpreexecute() {             super.onpreexecute();             // before making http calls         }          @override         protected void doinbackground(void... arg0) {             sharedpreference = new sharedpreferncesexecutor();             sharedpreference.save(context,"schooldata","updated data splash screen");             try {                 thread.sleep(3000);             } catch (interruptedexception e) {                 e.printstacktrace();             }             return null;         }          @override         protected void onpostexecute(void result) {             super.onpostexecute(result);             // after completing http call             // close activity , lauch main activity             intent = new intent(splashscreenactivitywithasynctask.this, mainactivity.class);//here main activity splash screen.             //i.putextra("now_playing", now_playing);             //i.putextra("earned", earned);             startactivity(i);              // close activity             finish();         }     } } 

please help.

you declaring local variable inside oncreate , assigning value of activity. member variable context not modified. stays null , npe. there no need create member variable. can instead of

sharedpreference.save(context,"schooldata","updated data splash screen"); 

use

sharedpreference.save(splashscreenactivitywithasynctask.this, "schooldata",                 "updated data splash screen"); 

the async task non-static inner class of splashscreenactivitywithasynctask has reference activity.


Comments

Popular posts from this blog

routing - AngularJS State management ->load multiple states in one page -

python - GRASS parser() error -

Swift game error message -