ankit rawat
ankit rawat

Reputation: 401

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 br = new BufferedReader(iz);

What could be going wrong?

*Edit: the printStackTrace

03-19 18:20:18.662: E/AndroidRuntime(929): Caused by: java.lang.NullPointerException

Edit 2: Code till the exception:

public class ListViewAa3 extends ListViewA{



public String[] process(String cti)throws IOException{
    String ctid=cti;
    Log.d("Outside try invoked","tag1");
    try{
        Log.d("beginning of try invoked","tag2");
        try{
    InputStream is = getAssets().open("USCOUNTIES.txt");
    InputStreamReader iz=new InputStreamReader(is);
    BufferedReader br = new BufferedReader(iz);}catch(Exception e){e.printStackTrace();}

Upvotes: 0

Views: 2858

Answers (3)

ankit rawat
ankit rawat

Reputation: 401

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.

Upvotes: 2

c.pramod
c.pramod

Reputation: 606

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 !

Upvotes: 0

user2173738
user2173738

Reputation:

The file that you open has not the full path, that's why the error. Try

new File("twentyone.txt").getAbsolutePath()

Upvotes: -1

Related Questions