Hello I am trying to call a method (fileReader) that is using openFileInput(filename) from another class.
I am doing some processing in a class which extends BroadcastReceiver, and I need to read a file, I can’t call openFileInput() inside of this class, so I have created a helper class which extends Activity or IntentService (tried both) so that I can instantiate it and call it like that:
HelperClass hc = new HelperClass();
hc.fileReader();
I get a NullPointerException on the line in fileReader where openFileInput is Called.
In my opinion it has to do something with the context or teh constructor not being properly initialized, its just HelperClass(){super();} .
How can I over come this?
When you are calling openFileInput you are trying to open a file associated with the given context. Since you are instantiating your HelperClass without supplying it a Context, it is trying to open your file using a null context (Hence your null pointer exception). What you can do is make your method
fileReader()accept a Context object and then call openFileInput using the provider context.Ex.
To call it
Alternatively, you can make fileReader a static method so you do not need to create an instance of HelperClass just to use it.
Then call it using