Исключение NullPointer в ContextWrapper

У меня есть класс FileGeneration, который расширяет Activity
В FileGeneration у меня есть метод, называемый

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;
}

И я получаю этот вывод в 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)

Строка 278 в классе

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

Но когда я просто печатаю метод с параметрами в Logcat, он говорит

openFileOutput(Preferences.xml, 1);

Файл не существует, но openFileOutput говорит, что создаст его, если он не существует

Что может быть не так?


person The87Boy    schedule 08.06.2011    source источник


Ответы (2)


Я решил проблему с (new ContextWrapper(ctx)).openFileOutput(this.fileName, cxt)

person The87Boy    schedule 26.06.2011

Попробуйте написать тест, чтобы узнать, есть ли this.filename == null, и вывести результат с помощью метода Log.d(String,String).

person kibibyte    schedule 08.06.2011
comment
Я пробовал это с помощью Log.d (ProgramName, openFileOutput (+ this.fileName +, + cxt +)); И это дало результат в вопросе - person The87Boy; 08.06.2011