java - Reading from assets gives null pointer -


i have file in assets folder want access in class i've named storagemanager (just regular class, doesn't extend anything):

public class storagemanager {     private context context;      public storagemanager(context context){         this.context=context;     }      public string readfileasstring(string filepath) throws java.io.ioexception {         assetmanager = context.getassets();         inputstream = am.open(filepath);         string results = "";         int data = is.read();         while (data !=-1) {             results += (char)data;             data = is.read();         }         is.close();         return results;     } } 

i'm creating instance of class within listfragment , passing getactivity() context.

however when make call readfileasstring valid filepath nullpointerexception line:

assetmanager = context.getassets();

i'm assuming means context null. why? how fix this?


edit

how called:

public class mylistfragment extends listfragment {     public view oncreateview(layoutinflater inflater, .....etc) {         ...         storagemanager storage = new storagemanager(getactivity());         try{             string filetext = readfileasstring("file");         }catch(...)}         ...     }  } 

try using getactivity().getbasecontext();


Comments

Popular posts from this blog

Unable to remove the www from url on https using .htaccess -