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