0

When I click back button in my app, I get a NullPointerException in getResource() method. getResource is used in the url and is called in the glideApp.

my code

private void setupPostUserImage(ImageView image, PostDetail detail) {
    mUserPicture.setVisibility(View.VISIBLE);
    String url = getActivity().getResources().getString(R.string.profile_image_path1) + "/" + detail.getUser_id_fk() + "/thumb/" + detail.getProfile_picture();
    //Toast.makeText(getContext(),url,Toast.LENGTH_LONG).show();
    GlideApp.with(image.getContext())
            .load(url)
            .placeholder(R.drawable.ic_avtar_male)
            .into(image);
}

This is my crash report from logcat

Process: com.ATG.World, PID: 24491
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.support.v4.app.FragmentActivity.getResources()' on a null object reference
    at com.ATG.World.fragments.ArticleDetailFragment2.setupPostUserImage(ArticleDetailFragment2.java:316)
    at com.ATG.World.fragments.ArticleDetailFragment2.setData(ArticleDetailFragment2.java:301)
    at com.ATG.World.fragments.ArticleDetailFragment2$1.onResponse(ArticleDetailFragment2.java:176)
    at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:71)
    at android.os.Handler.handleCallback(Handler.java:898)
    at android.os.Handler.dispatchMessage(Handler.java:107)
    at android.os.Looper.loop(Looper.java:198)
    at android.app.ActivityThread.main(ActivityThread.java:6716)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
12-21 19:38:17.829 24491-24491/com.ATG.World I/Process: Sending signal. PID: 24491 SIG: 9

what should I do in order to resolve this exception?

Rakesh
  • 4,004
  • 2
  • 19
  • 31
  • 2
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Murat Karagöz Dec 21 '18 at 14:28
  • I went through this but cannot find solution for my problem. – jilla sivaram Dec 21 '18 at 14:32
  • Sounds like your `getActivity()` is returning null because your fragment was destroyed – tyczj Dec 21 '18 at 14:36
  • Your code have possibility of breaking at every dot( . ). If getActivity() is null it breaks. If getActivity().getResources() is null it breaks. So before using dot operator on any object do a null check and then access the object / function. – Mukesh Verma Dec 21 '18 at 14:37
  • @MukeshVerma null check worked for me. thanks for the help – jilla sivaram Dec 22 '18 at 15:02

1 Answers1

0

I don't have reputation to comment so I have to write answer. It looks like your fragment is not attached to activity so method getActivity() returns null. Look this answer may be it will be useful getActivity() returns null in Fragment function