3

I have a class called FileGeneration that extends Activity
In FileGeneration I have a method called

protected OutputStream openAndWriteFile() {

   // Set the Context-mode
   int cxt = Context.MODE_PRIVATE;

   // Check if we are not going to clear the file and the file exists
   if (!clearFile && (new File(this.fileName)).exists()) {

      // Append to the file
      cxt = Context.MODE_APPEND;
   }

   // Try to open the file to write to
   try {

      // Open the File using the Context
      this.os = openFileOutput(this.fileName, cxt);

   } catch (FileNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
   }

   // Return the OutputStream
   return this.os;
}

And I get this output in Logcat

06-08 15:31:43.733: ERROR/AndroidRuntime(2850): java.lang.NullPointerException
06-08 15:31:43.733: ERROR/AndroidRuntime(2850):    at android.content.ContextWrapper.openFileOutput(ContextWrapper.java:158)
06-08 15:31:43.733: ERROR/AndroidRuntime(2850):    at android.content.ContextWrapper.openFileOutput(ContextWrapper.java:158)
06-08 15:31:43.733: ERROR/AndroidRuntime(2850):    at dataconnection.FileGeneration.openAndWriteFile(FileGeneration.java:278)

Line 278 in the class is

this.os = openFileOutput(this.fileName, cxt);

But when I just print the method with the paramters out in Logcat it says

openFileOutput(Preferences.xml, 1);

The file does not exist, but openFileOutput says it will create it, if it does not exist

What can be wrong?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
The87Boy
  • 877
  • 4
  • 14
  • 32

2 Answers2

1

I solved the problem with (new ContextWrapper(ctx)).openFileOutput(this.fileName, cxt)

The87Boy
  • 877
  • 4
  • 14
  • 32
0

Try writing a test to see if this.filename == null, and output the result using the Log.d(String,String) method.

kibibyte
  • 3,007
  • 5
  • 30
  • 40
  • I have tried this by using Log.d("ProgramName", "openFileOutput(" + this.fileName + ", " + cxt + ")"); And that gave the output in the question – The87Boy Jun 08 '11 at 17:34