Skip to content Skip to sidebar Skip to footer

Inputstream Returning Nullpointerexception (java)

Here is the code that is getting the NullPointerException error: InputStream is = getAssets().open('twentyone.txt'); InputStreamReader iz=new InputStreamReader(is); BufferedReader

Solution 1:

OK, I got it. I had to pass the context of the main activity to this class, then use context.getAssets.open("twentyone.txt");

To anyone who has the same problem, do this: Put this in the onCreate function of the class with activity: Context context=getApplicationContext();

Pass context to the new class's function(in my case "process(String a,Context context)") Then type this in the process function:

InputStream is = context.getAssets().open("twentyone.txt");

Took me 4 hours to figure such a silly thing out.

Solution 2:

Maybe getAssets() is null and try to Check whether file twentyone.txt exists , if it exists then try to enter the full file path and re-run your application !

Post a Comment for "Inputstream Returning Nullpointerexception (java)"