title: android write data to file internal,method in class
body: <p>I try to write data in a txt file, using the internal storage of android,
  it works fine when i use the code directly in the "buttonClicked" function in the mainActivity.java but when i try to call the method in a class the emulator crashes. Thanks!</p>
  
  <pre><code>public class MainActivity extends Activity {
  
          // the class object ----------------------
      FileHandler myFileHandler = new FileHandler();
      public static final String FILENAME = "myFile.txt";
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
      }
  
  
     public void buttonClicked(View view) {
         // Call the method in the Filehandler Class 
         myFileHandler.saveData(FILENAME);
  } ...
  </code></pre>
  
  <p>fileHandler Class</p>
  
  <pre><code>public class FileHandler extends Activity {
      private static final String TAG = MainActivity.class.getName();
  
      FileHandler() { 
      }
  
  
      void saveData(String FILENAME) {
  
          Log.d(TAG, FILENAME);
  
          try {
                 OutputStreamWriter outputStreamWriter = new OutputStreamWriter(openFileOutput(FILENAME, Context.MODE_PRIVATE));
                 outputStreamWriter.write("hallo welt");
                 outputStreamWriter.close();
             }
             catch (IOException e) {
                 Log.e(TAG, "File write failed: " + e.toString());
             }
      } ...
  </code></pre>
  
  <p>this works fine …</p>
  
  <pre><code>public void buttonClicked(View view) {
  
  
  
         myFileHandler.saveData(FILENAME);
  
          try {
                 OutputStreamWriter outputStreamWriter = new OutputStreamWriter(openFileOutput(FILENAME, Context.MODE_PRIVATE));
                 outputStreamWriter.write("hallo welt");
                 outputStreamWriter.close();
             }
             catch (IOException e) {
                 Log.e(TAG, "File write failed: " + e.toString());
             } 
  
      }
  </code></pre>
  
  <p>all the classes are imported correctly, the error from log cat</p>
  
  <pre><code>    11-11 11:49:22.646: E/AndroidRuntime(833): FATAL EXCEPTION: main
  11-11 11:49:22.646: E/AndroidRuntime(833): java.lang.IllegalStateException: Could not execute method of the activity
  11-11 11:49:22.646: E/AndroidRuntime(833):  at android.view.View$1.onClick(View.java:2072)
  11-11 11:49:22.646: E/AndroidRuntime(833):  at android.view.View.performClick(View.java:2408)
  11-11 11:49:22.646: E/AndroidRuntime(833):  at android.view.View$PerformClick.run(View.java:8816)
  11-11 11:49:22.646: E/AndroidRuntime(833):  at android.os.Handler.handleCallback(Handler.java:587)
  11-11 11:49:22.646: E/AndroidRuntime(833):  at android.os.Handler.dispatchMessage(Handler.java:92)
  11-11 11:49:22.646: E/AndroidRuntime(833):  at android.os.Looper.loop(Looper.java:123)
  11-11 11:49:22.646: E/AndroidRuntime(833):  at android.app.ActivityThread.main(ActivityThread.java:4627)
  11-11 11:49:22.646: E/AndroidRuntime(833):  at java.lang.reflect.Method.invokeNative(Native Method)
  11-11 11:49:22.646: E/AndroidRuntime(833):  at java.lang.reflect.Method.invoke(Method.java:521)
  11-11 11:49:22.646: E/AndroidRuntime(833):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
  11-11 11:49:22.646: E/AndroidRuntime(833):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
  11-11 11:49:22.646: E/AndroidRuntime(833):  at dalvik.system.NativeStart.main(Native Method)
  11-11 11:49:22.646: E/AndroidRuntime(833): Caused by: java.lang.reflect.InvocationTargetException
  11-11 11:49:22.646: E/AndroidRuntime(833):  at com.example.iLoveMyMobile.MainActivity.buttonClicked(MainActivity.java:55)
  11-11 11:49:22.646: E/AndroidRuntime(833):  at java.lang.reflect.Method.invokeNative(Native Method)
  11-11 11:49:22.646: E/AndroidRuntime(833):  at java.lang.reflect.Method.invoke(Method.java:521)
  11-11 11:49:22.646: E/AndroidRuntime(833):  at android.view.View$1.onClick(View.java:2067)
  11-11 11:49:22.646: E/AndroidRuntime(833):  ... 11 more
  11-11 11:49:22.646: E/AndroidRuntime(833): Caused by: java.lang.NullPointerException
  11-11 11:49:22.646: E/AndroidRuntime(833):  at android.content.ContextWrapper.openFileOutput(ContextWrapper.java:158)
  11-11 11:49:22.646: E/AndroidRuntime(833):  at com.example.iLoveMyMobile.FileHandler.saveData(FileHandler.java:36)
  11-11 11:49:22.646: E/AndroidRuntime(833):  ... 15 more
  </code></pre>