title: How to get hashmap response using Retrofit 2
body: <p>I am trying to fetch json object response using retrofit 2. I used hashmap as the keys are dynamic. Here is my response class :</p>
  
  <pre><code>public class Countries {
  
      private Map&lt;String, Model&gt; datas;
  
      public Map&lt;String, Model&gt; getDatas() {
          return datas;
      }
  }
  </code></pre>
  
  <p>And the <code>Model</code> class is :</p>
  
  <pre><code>public class Model {
  
      @SerializedName("country_name")
      private String country_name;
      @SerializedName("continent_name")
      private String continent_name;
  
      public String getCountry_name() {
          return country_name;
      }
  
      public String getContinent_name() {
          return continent_name;
      }
  }
  </code></pre>
  
  <p>So far, i've tried to handle the response like this : </p>
  
  <pre><code>call.enqueue(new Callback&lt;Countries&gt;() {
              @Override
              public void onResponse(Call&lt;Countries&gt; call, Response&lt;Countries&gt; response) {
  
                  Map&lt;String, Model&gt; map = new HashMap&lt;String, Model&gt;();
                  map = response.body().getDatas();
  
                  for (String keys: map.keySet()) {
                      // myCode;
                  }
  
              }
  
              @Override
              public void onFailure(Call&lt;Countries&gt; call, Throwable t) {
  
              }
          });
  </code></pre>
  
  <p>And this error occurs : </p>
  
  <blockquote>
    <p>java.lang.NullPointerException: Attempt to invoke interface method
    'java.util.Set java.util.Map.keySet()' on a null object reference</p>
  </blockquote>
  
  <p>JSON response looks like this :</p>
  
  <pre><code>{
      "0": {
          "country_name": "Argentina",
          "continent_name": "South America"
      },
      "1": {
          "country_name": "Germany",
          "continent_name": "Europe"
      }
  }
  </code></pre>
  
  <p>So how can I get the response in HashMap?</p>