简体   繁体   中英

Android: openFileOutput throws NullPointerException

I'm trying to write Strings into a file and for that i use openFileOutput:

 FileOutputStream fOut =  openFileOutput("samplefile.txt",Context.MODE_PRIVATE);

For some reason the app crashes due to a NullPointerException (I read that it happens on the emulator and not on the actual device so I hooked my phone and guess what - CRASHED TOO :-( )

here is the LogCat output:

04-18 22:48:25.520: E/AndroidRuntime(12045): FATAL EXCEPTION: main
04-18 22:48:25.520: E/AndroidRuntime(12045): java.lang.NullPointerException
04-18 22:48:25.520: E/AndroidRuntime(12045):    at    android.content.ContextWrapper.openFileOutput(ContextWrapper.java:165)
04-18 22:48:25.520: E/AndroidRuntime(12045):    at     com.example.tester.GenerateXml.listToTextFile(GenerateXml.java:48)
04-18 22:48:25.520: E/AndroidRuntime(12045):    at com.example.tester.MainActivity$1.parseAppListToXML(MainActivity.java:81)
04-18 22:48:25.520: E/AndroidRuntime(12045):    at com.example.tester.MainActivity$1.onClick(MainActivity.java:62)
04-18 22:48:25.520: E/AndroidRuntime(12045):    at android.view.View.performClick(View.java:3517)
04-18 22:48:25.520: E/AndroidRuntime(12045):    at android.view.View$PerformClick.run(View.java:14155)
04-18 22:48:25.520: E/AndroidRuntime(12045):    at android.os.Handler.handleCallback(Handler.java:605)
04-18 22:48:25.520: E/AndroidRuntime(12045):    at android.os.Handler.dispatchMessage(Handler.java:92)
04-18 22:48:25.520: E/AndroidRuntime(12045):    at android.os.Looper.loop(Looper.java:154)
04-18 22:48:25.520: E/AndroidRuntime(12045):    at android.app.ActivityThread.main(ActivityThread.java:4624)
04-18 22:48:25.520: E/AndroidRuntime(12045):    at java.lang.reflect.Method.invokeNative(Native Method)
04-18 22:48:25.520: E/AndroidRuntime(12045):    at java.lang.reflect.Method.invoke(Method.java:511)
04-18 22:48:25.520: E/AndroidRuntime(12045):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
04-18 22:48:25.520: E/AndroidRuntime(12045):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
04-18 22:48:25.520: E/AndroidRuntime(12045):    at dalvik.system.NativeStart.main(Native Method)

here is my code:

package com.example.tester;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.util.Log;
import android.util.Xml;

public class GenerateXml extends Activity{

private List<ApplicationInfo> packages;
private static final String TAG = MainActivity.class.getName();
private static final String FILENAME = "myFile.txt";

public void listToTextFile(List<ApplicationInfo> _packages) {


    try { // catches IOException below
         final String TESTSTRING = _packages.toString();

         // ##### Write a file to the disk #####
         /* We have to use the openFileOutput()-method
          * the ActivityContext provides, to
          * protect your file from others and
          * This is done for security-reasons.
          * We chose MODE_WORLD_READABLE, because
          *  we have nothing to hide in our file */             
      //   

         FileOutputStream fOut =       openFileOutput("samplefile.txt",Context.MODE_PRIVATE);

         OutputStreamWriter osw = new OutputStreamWriter(fOut); 

         // Write the string to the file
         osw.write(TESTSTRING);
         /* ensure that everything is
          * really written out and close */
         osw.flush();
         osw.close();
     }catch (IOException e){
         //Log.e(TAG,"could not open file out stream", e); 
     }
}

}

Any ideas?

Seems like you've instantiated your GenerateXml activity with new GenerateXml() . You cannot instantiate activities that way - they won't be properly set up as Context s and so on.

From the code you posted, it seems like GenerateXml should not be an Activity at all. Remove the extends Activity and pass in a Context as an argument where it's needed, for example:

public void listToTextFile(Context context, List<ApplicationInfo> _packages) {
    //...
    ... context.openFileOutput(...);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM