title: No resource ID found, when resource exists
body: <p>I am creating an app, that fetchs data from JSON, and i got stuck on one part: When i try to read my image from string to <code>ImageView</code>, i get error:</p>
  
  <pre><code>E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #2
                Process: , PID: 9535
                java.lang.RuntimeException: An error occured while executing doInBackground()
                    at android.os.AsyncTask$3.done(AsyncTask.java:304)
                    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
                    at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
                    at java.util.concurrent.FutureTask.run(FutureTask.java:242)
                    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
                    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
                    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
                    at java.lang.Thread.run(Thread.java:818)
                 Caused by: java.lang.RuntimeException: No resource ID found for: i_coal / class android.graphics.drawable.Drawable
                    at com.infonuascape.osrshelper.fragments.BankViewFragment.getResId(BankViewFragment.java:69)
                    at com.infonuascape.osrshelper.fragments.BankViewFragment$GetItems.doInBackground(BankViewFragment.java:100)
                    at com.infonuascape.osrshelper.fragments.BankViewFragment$GetItems.doInBackground(BankViewFragment.java:74)
                    at android.os.AsyncTask$2.call(AsyncTask.java:292)
                    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
                    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
                    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
                    at java.lang.Thread.run(Thread.java:818) 
                 Caused by: java.lang.NoSuchFieldException: i_coal
                    at java.lang.Class.getDeclaredField(Class.java:890)
                    at .fragments.BankViewFragment.getResId(BankViewFragment.java:66)
                    at .fragments.BankViewFragment$GetItems.doInBackground(BankViewFragment.java:100) 
                    at .fragments.BankViewFragment$GetItems.doInBackground(BankViewFragment.java:74) 
                    at android.os.AsyncTask$2.call(AsyncTask.java:292) 
                    at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
                    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
                    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
                    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
                    at java.lang.Thread.run(Thread.java:818) 
  </code></pre>
  
  <p>How can i fix it? The resource exists if i try to use R.Drawable.i_coal;
  Also how can i check if the resource does not exist to skip it instead of throwing an error?</p>
  
  <p><code>BankViewFragment</code>:</p>
  
  <pre><code>import android.content.Context;
  
  public class BankViewFragment {
  private static final String TAG = "BankViewFragment";
  
  private static Account account;
  private ListView lv;
  private ImageView iv;
  
  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);
      iv = (ImageView) view.findViewById(R.id.logo);
      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);
                      String amount = line.getString("amount");
                      Log.e(TAG, "DAIGTAS: " + 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);
                      ImageView ims = (ImageView) lv.findViewById(R.id.logo);
                      lv.setBackgroundResource(getResId(item, Drawable.class));
                      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><code>list_item.xml</code>:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:orientation="horizontal"
  android:padding="@dimen/activity_horizontal_margin"&gt;
  
  &lt;ImageView
      android:id="@+id/logo"
      android:layout_height="match_parent"
      android:layout_weight="1"
      app:srcCompat="@android:drawable/btn_star"
      android:layout_width="0dp" /&gt;
  
  &lt;TextView
      android:id="@+id/name"
      android:layout_width="219dp"
      android:layout_height="wrap_content"
      android:paddingBottom="2dip"
      android:paddingTop="6dip"
      android:textColor="@color/black"
      android:textSize="16sp"
      android:textStyle="bold" /&gt;
  
  &lt;TextView
      android:id="@+id/email"
      android:layout_width="83dp"
      android:layout_height="match_parent"
      android:paddingBottom="2dip"
      android:textColor="@color/black" /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>