(Currently 52) programming from 51 years old Note FileOutputStream android.content.Context.openFileOutput (java.lang.String, int)'on a null object reference

Suppose you have this code

SampleActivity


Context context;
@Override 
protected void onCreate(Bundle savedInstanceState){
   super.onCreate(savedInstanceState);
   setContantView(R.layout.sample);
   context = getApplicationContext();

   new AsyncTask().execute();
}


public void fileWrite(){
   try{
       openFileOutput("fileName",Context.MODE_APPEND);
   }
}

AsyncTask


@Override 
protected void onPostExecute(String s){
   super.onPostExecute(s);

   SampleActivity sampleA = new SampleActivity();
   sampleA.fileWrite();
}

With this FileOutputStream android.content.Context.openFileOutput(java.lang.String, int)' on a null object reference I get an error.

Since context is not generated when the instance is created in the code, it becomes NullPointerExeption.

If you want to avoid it> https://qiita.com/old_cat/items/ff4f2116192fd536fb59 As described in, implement the callback method and handle the context.

SampleActivity


Context context;
@Override 
protected void onCreate(Bundle savedInstanceState){
   super.onCreate(savedInstanceState);
   setContantView(R.layout.sample);
   context = getApplicationContext();

   AsyncTaskCallBack asyncTaskCallBack = new AsyncTaskCallBack();
   AsyncTask asyncTask = new AsyncTask(asyncTaskCallBack);
}

public class AsyncTaskCallBack(){
   //You can also use fileOutputStream here
   //But try calling another method
   public void one(){
      fileWriter();
   }
}

public void fileWrite(){
   try{
       openFileOutput("fileName",Context.MODE_APPEND);
   }
}

AsyncTask


SampleActivity.AsyncTaskCallBack asyncTaskCallBack;

public AsyncTask(SampleActivity.AsyncTaskCallBack asyncTaskCallBack){
   this.asyncTaskCallBack = asyncTaskCallBack;
}

@Override 
protected void onPostExecute(String s){
   super.onPostExecute(s);
   asyncTaskCallBack.one();
}

Recommended Posts

(Currently 52) programming from 51 years old Note FileOutputStream android.content.Context.openFileOutput (java.lang.String, int)'on a null object reference
Programming from 51 years old Note --reference
Programming from 51 years old Note Service --reference
Programming from 51 years old (currently 52) Note Path and File
Programming from 51 years old Note AsyncTask --reference [copying sutras]
Programming from 51 years old Note Thread summary
(Currently 52) programming from 51 years old Note 3 lines Move focus to android button
Programming from 51 years old (currently 52) memorandum VScode javadoc
Programming from 51 years old (currently 52) Note: Add jar with VScode Class.forName java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Drive
Programming memorandum process thread from 51 years old Handler -reference
Programming from 51 years old Note: Checking Android network connection
Programming from 51 years old Note: Background execution restrictions OverView [copying sutras]
Programming from 51 years old memorandum android Timer
Programming from 51 years old [copying sutras] Intent IntentFilter-reference
Programming from 51 years old Android memo Activity Manager memo
Programming from 51 years old Note: Do not start Android Sevice more than once Service start confirmation