title: Attempt to invoke virtual method on a null object reference?
body: <p>I have code that gets an image by its name from <code>drawables</code>, but for some reason it can't update it.</p>
  
  <pre><code>package com.infonuascape.osrshelper.fragments;
  
  import android.content.Context;
  import android.content.SharedPreferences;
  import android.graphics.drawable.Drawable;
  import android.media.Image;
  import android.os.AsyncTask;
  import android.os.Bundle;
  import android.os.Handler;
  import android.os.Looper;
  import android.support.annotation.Nullable;
  import android.util.Log;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.ImageView;
  import android.widget.ListAdapter;
  import android.widget.ListView;
  import android.widget.SimpleAdapter;
  import android.widget.TextView;
  import android.widget.Toast;
  
  import com.android.volley.toolbox.ImageLoader;
  import com.infonuascape.osrshelper.R;
  import com.infonuascape.osrshelper.activities.MainActivity;
  import com.infonuascape.osrshelper.models.Account;
  
  import org.json.JSONException;
  import org.json.JSONObject;
  
  import java.lang.reflect.Field;
  import java.util.ArrayList;
  import java.util.HashMap;
  import java.util.Iterator;
  
  public class BankViewFragment extends OSRSFragment {
      private static final String TAG = "BankViewFragment";
  
      private static Account account;
      private ListView lv;
      private ImageView iv;
      Handler handler;
  
      ArrayList&lt;HashMap&lt;String, String&gt;&gt; ItemList;
  
      public static BankViewFragment newInstance(final Account account) {
          BankViewFragment fragment = new BankViewFragment();
          Bundle b = new Bundle();
          fragment.setArguments(b);
          return fragment;
      }
  
  
      @Nullable
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
          super.onCreateView(inflater, container, savedInstanceState);
          View view = inflater.inflate(R.layout.bank_view, null);
          ItemList = new ArrayList&lt;&gt;();
          new GetItems().execute();
          lv = (ListView) view.findViewById(R.id.list);
          handler = new Handler(Looper.getMainLooper());
          SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
          String nikas = sharedPref.getString("bankname", "null");
          return view;
      }
  
      public static int getResId(String resourceName, Class&lt;?&gt; c) {
          try {
              Field idField = c.getDeclaredField(resourceName);
              return idField.getInt(idField);
          } catch (Exception e) {
              throw new RuntimeException("No resource ID found for: "
                      + resourceName + " / " + c, e);
          }
      }
  
      private class GetItems extends AsyncTask&lt;Void, Void, Void&gt; {
          @Override
          protected void onPreExecute() {
              super.onPreExecute();
          }
  
          @Override
          protected Void doInBackground(Void... arg0) {
              HttpHandler sh = new HttpHandler();
              SharedPreferences sharedpreferences = getContext().getSharedPreferences("minescape", Context.MODE_PRIVATE);
              String nikas = sharedpreferences.getString("bankname",  "null");
              String url = "https://api.minesca.pe/game/classic/stats?username=" + nikas;
              String jsonStr = sh.makeServiceCall(url);
  
              Log.e(TAG, "NIKAS: " + nikas);
              Log.e(TAG, "ACCOUNT: " + account);
              Log.e(TAG, "Response from url: " + jsonStr);
              if (jsonStr != null) {
                  try {
                      JSONObject jsonObj = new JSONObject(jsonStr);
                      JSONObject items = jsonObj.getJSONObject("bank");
                      Iterator keys = items.keys();
                      while(keys.hasNext()) {
                          String dynamicKey = (String)keys.next();
                          JSONObject line = items.getJSONObject(dynamicKey);
                          String item = line.getString("item");
                          //Integer image = getResId(item, Drawable.class);
                          final Integer image = getResources().getIdentifier(item, "drawable", getActivity().getPackageName());
                          String amount = line.getString("amount");
                          Log.e(TAG, "DAIKTAS: " + item);
                          Log.e(TAG, "KIEKIS: " + amount);
                          HashMap&lt;String, String&gt; contact = new HashMap&lt;&gt;();
                          String itembank = item.replaceAll("i_", "");
                          String itembanks = itembank.replaceAll("_", " ");
                          contact.put("name", itembanks);
                          contact.put("email", amount);
                          LayoutInflater inflater = LayoutInflater.from(getContext());
                          View view = inflater.inflate(R.layout.bank_view, null);
                          lv = (ListView) view.findViewById(R.id.list);
                         // iv = (ImageView) view.findViewById(R.id.logo);
                          final ImageView ims = (ImageView) lv.findViewById(R.id.logo);
                          handler.post(new Runnable() {
                              public void run() {
                                  if(image != null) {
                                      Log.e(TAG, "kas cia jam netinka?: " + image);
                                      if(image == 0) {
                                          ims.setImageResource(R.drawable.i_noted);
                                          Log.e(TAG, "kas cia jam netinka?: " + image);
                                      } else {
                                          Log.e(TAG, "kas cia jam netinka?: " + image);
                                          ims.setImageResource(image);
                                      }
                                  } else {
                                      Log.e(TAG, "null?: " + image);
                                  }
                              }
                          });
                          ItemList.add(contact);
                      }
                  } catch (final JSONException e) {
                      Log.e(TAG, "Json parsing error: " + e.getMessage());
                      new Runnable() {
                          @Override
                          public void run() {
                              Toast.makeText(getContext(),
                                      "Json parsing error: " + e.getMessage(),
                                      Toast.LENGTH_LONG).show();
                          }
                      };
  
                  }
  
              } else {
                  Log.e(TAG, "Couldn't get json from server.");
                  new Runnable() {
                      @Override
                      public void run() {
                          Toast.makeText(getContext(),
                                  "Couldn't get json from server!",
                                  Toast.LENGTH_LONG).show();
                      }
                  };
              }
  
              return null;
          }
  
          @Override
          protected void onPostExecute(Void result) {
              super.onPostExecute(result);
              ListAdapter adapter = new SimpleAdapter(getContext(), ItemList,
                      R.layout.list_item, new String[]{ "email","name"},
                      new int[]{R.id.email, R.id.name});
              lv.setAdapter(adapter);
          }
      }
  
  }
  </code></pre>
  
  <p><strong>Error</strong>:</p>
  
  <pre><code>E/AndroidRuntime: FATAL EXCEPTION: main
                Process: com.infonuascape.osrshelper, PID: 31024
                java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' on a null object reference
                    at com.infonuascape.osrshelper.fragments.BankViewFragment$GetItems$1.run(BankViewFragment.java:128)
                    at android.os.Handler.handleCallback(Handler.java:815)
                    at android.os.Handler.dispatchMessage(Handler.java:104)
                    at android.os.Looper.loop(Looper.java:194)
                    at android.app.ActivityThread.main(ActivityThread.java:5637)
                    at java.lang.reflect.Method.invoke(Native Method)
                    at java.lang.reflect.Method.invoke(Method.java:372)
                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
  </code></pre>
  
  <p>The problem is that there's a lot of items, and most of them have the resources, but some don't and i would like to just skip those who don't have, but the main problem is that the app crashes while trying to see the <strong>bank</strong>.</p>
  
  <p>Yes i have tried to solve my question using similar posts(duplicates), but none of them helped me.</p>