Исключение NullPointer в ContextWrapper - PullRequest
3 голосов
/ 08 июня 2011

У меня есть класс с именем 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 сообщает, что создаст его, если он не существует

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

Ответы [ 2 ]

1 голос
/ 26 июня 2011

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

0 голосов
/ 08 июня 2011

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

...