Skip to content Skip to sidebar Skip to footer

Nullpointerexception With Inherited Function From A Class

I am trying to inherit a function from a class, but it returns NullPointerException. Can anyone please help? This is the function I try to inherit in Main.java. This function opera

Solution 1:

The problem is that openFileInput() needs a Context. You need to create a constructor in your class that takes context as a param.

publicvoid ClassWithFunction
{
   Context mContext;
    publicClassWithFunction(Context context){
         mContext = context
    }
}

then pass your Context

Mainmain=newMain(this);
main.readData();

then use that when you call this function

FileInputStreamfIn= mContext.openFileInput("user.txt");  //mContext is context sent from Activity

Note You will want to use this Context if you have any other methods in this class that need it

Here is an answer of mine on SO about the same thing

Post a Comment for "Nullpointerexception With Inherited Function From A Class"