Reputation: 2948
why sometimes this code is throwing errors and sometime not ??
if ( getActivity().getApplicationContext() != null ){
File file = new File(getActivity().getApplicationContext().getFilesDir() + "/img.jpg"); }
the above code is inside my fragment
my Error:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.support.v4.app.FragmentActivity.getApplicationContext()' on a null object reference
Upvotes: 0
Views: 77
Reputation: 1287
If you are running this in seperate AsyncTask or thread and activity that has started this no longer exists or active. getActivity() might return null.
So solution is to add null check for getActivity() too along with getActivity().getApplicationContext().
Upvotes: 3