-1

I'm trying to write a file from another class, but I get a nullpointerexception that I can not solve. The following code can be used in the mainactivity without any problems, but when I try to use it in my second class, I get the error.

 public void writeFile() {

    String text = "Hello world";
    String fileName = "TestFile.txt";


    FileOutputStream fileOutputStream;
    MainActivity maObj = new MainActivity();


    try {

        fileOutputStream = maObj.openFileOutput(fileName, maobj.MODE_PRIVATE);
        fileOutputStream.write(text.getBytes());
        fileOutputStream.close();


    } catch (IOException e) {

        e.printStackTrace();


    }
}
Jullix993
  • 105
  • 10

3 Answers3

4

You need to pass activity context instead of creating new activity instance in write method as

public void writeFile(Context context) { 

and remove MainActivity maObj = new MainActivity(); as Activities can not be instantiated this way in Android. Further, change

fileOutputStream = maObj.openFileOutput(fileName, maobj.MODE_PRIVATE);

to

    fileOutputStream = context.openFileOutput(fileName, Context.MODE_PRIVATE);

Once complete, your method should look like this:

public void writeFile( Context context ) {

    String text = "Hello world";
    String fileName = "TestFile.txt";

    FileOutputStream fileOutputStream;

    try {

        fileOutputStream = context.openFileOutput(fileName, Context.MODE_PRIVATE);
        fileOutputStream.write(text.getBytes());
        fileOutputStream.close();

    } catch (IOException e) {
        e.printStackTrace();
    }
}

Now Call your method as

writeFile(MainActivity.this);

from MainActivity class.

323go
  • 14,143
  • 6
  • 33
  • 41
Giru Bhai
  • 14,370
  • 5
  • 46
  • 74
  • @Jullix993 Happy to help,enjoy. – Giru Bhai Mar 16 '15 at 17:51
  • This has potential to be a good answer -- after all, it is correct. Please add explanations, remove the "try" (that sounds experimental), and sum up the new `writeFile()` method. – 323go Mar 16 '15 at 17:56
  • @323go What do you mean by remove the "try"? It has to throw an exception, right? – Jullix993 Mar 16 '15 at 18:23
  • @Jullix993, the answer included a "try to do this..." phrase, which is more appropriate for comments than answers. The answerer has removed it, which makes this a very good answer. +1 – 323go Mar 16 '15 at 20:52
2

you cannot create an Activity with a new Constructor like you did here.

MainActivity maObj = new MainActivity();
Simulant
  • 19,190
  • 8
  • 63
  • 98
  • 1
    actually you can, but the android OS will not use it to make the activity life cycle , and btw this is not the answer – zaPlayer Mar 16 '15 at 17:50
  • @SamerZuhair, no you can *not*. – 323go Mar 16 '15 at 17:54
  • @323go are you telling me that i can't say Object mObj=new Object and that's what i say you can do; or are you telling me that i can't attach MainActivity maObj = new MainActivity(); maObj to the android OS which i'm telling you that the android will not allow it – zaPlayer Mar 16 '15 at 18:02
-2

change to this

fileOutputStream = this.openFileOutput(fileName, maobj.MODE_PRIVATE);

zaPlayer
  • 787
  • 5
  • 24
  • make sure you are in the activity class – zaPlayer Mar 16 '15 at 17:48
  • Why would you put "this" there? Why would I refer to the class object when I need to refer to the mainactivity? – Jullix993 Mar 16 '15 at 18:20
  • @Jullix993 if you took a look at my first comment, you would understand also you could check google code here http://developer.android.com/training/basics/firstapp/starting-activity.html in building the intent they wrote Intent intent = new Intent(this, DisplayMessageActivity.class); when they actually needed a context(google convention) – zaPlayer Mar 16 '15 at 18:33