I have this function in my class Activity :
private Bitmap getBitmapFromAsset(String strName) {
AssetManager assetManager =this.getAssets();
InputStream istr = null;
try {
istr = assetManager.open(strName);
} catch (IOException e) {
e.printStackTrace();
}
Bitmap bitmap = BitmapFactory.decodeStream(istr);
return bitmap;
}
this function is in public class Game1 extends Activity
but I get nullpointer exception on :
AssetManager assetManager = this.getAssets();
Caused by: java.lang.NullPointerException at packagename.Game1.getBitmapFromAsset
what should I do?
You need to call getBitmapFromAsset()
after the activity is created.
If you call it before the activity creation then it will give NullPointerException
.
The code then -
AssetManager assetManager = this.getAssets();
Here the this = getActivity()
and therefore is null.