title: Exception in Async Task
body: <p><a href="https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it">What is a NullPointerException, and how do I fix it?</a> I'm asking this question after referring many answers regarding null pointer exception in Stack Overflow. I tried many methods and failed. </p>
  
  <p>So the issue is I'm getting null pointer exception in Async task. I'm gonna leave the piece of codes that is responsible for this error.</p>
  
  <p>Below is the class where I'm getting an error.</p>
  
  <p><strong>GetArrayAsyncTask :</strong></p>
  
  <pre><code>@TargetApi(19)
  public Map doInBackground(String... strArr) {
      StringBuffer stringBuffer = new StringBuffer();
      String str = null;
      try {
          HttpURLConnection httpURLConnection = (HttpURLConnection) new URL(strArr[0]).openConnection();
          httpURLConnection.setRequestMethod("GET");
          httpURLConnection.connect();
          InputStream inputStream = httpURLConnection.getInputStream();
          InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
          BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
          while (true) {
              String readLine = bufferedReader.readLine();
              if (readLine == null) {
                  break;
              }
              stringBuffer.append(readLine);
          }
          bufferedReader.close();
          inputStreamReader.close();
          inputStream.close();
          httpURLConnection.disconnect();
          String stringBuffer2 = stringBuffer.toString();
          if (!stringBuffer2.contains("host") &amp;&amp; !stringBuffer2.contains("version")) {
              boolean contains = stringBuffer2.contains("http");
          }
          List list = JsonHelper.toList(new JSONArray(stringBuffer2));
          HashMap hashMap = new HashMap();
          hashMap.put("data", list);
          return hashMap;
      } catch (MalformedURLException unused) {
      } catch (ProtocolException unused2) {
      } catch (IOException unused3) {
      } catch (JSONException unused) {
          return (Map) JsonUtils.stringObject(str, Map.class); //Error is here.
      }
      return null;
  }
  
  public void onPostExecute(Map map) {
      if (map == null || map.size() &lt;= 0) {
          Toast.makeText(this.context, "Data format error!", Toast.LENGTH_LONG).show();
          this.listener.onGetArrayResultsData(map, this.request_code);
          return;
      }
      this.listener.onGetArrayResultsData(map, this.request_code);
  }
  </code></pre>
  
  <p>The exception occurs at <code>return (Map) JsonUtils.stringObject(str, Map.class);</code></p>
  
  <p><strong>JsonUtils :</strong></p>
  
  <pre><code>public class JsonUtils {
  public static String jsonString(Object obj) {
      try {
          return new ObjectMapper().writeValueAsString(obj);
      } catch (JsonProcessingException unused) {
          return null;
      }
  }
  
  public static &lt;T&gt; T stringObject(String str, Class&lt;T&gt; cls) {
      try {
          return new ObjectMapper().readValue(str, cls); //Error line.
      } catch (JsonParseException unused) {
          return null;
      } catch (JsonMappingException unused2) {
          return null;
      } catch (IOException unused3) {
          return null;
      }
  }
  }
  </code></pre>
  
  <p><strong>LogCat :</strong></p>
  
  <pre><code>java.lang.RuntimeException: An error occurred while executing doInBackground()
      at android.os.AsyncTask$3.done(AsyncTask.java:318)
      at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
      at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
      at java.util.concurrent.FutureTask.run(FutureTask.java:242)
      at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
      at java.lang.Thread.run(Thread.java:760)
   Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
      at com.fasterxml.jackson.core.JsonFactory.createParser(JsonFactory.java:889)
      at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3004)
      at converter.JsonUtils.stringObject(JsonUtils.java:20)
      at com.kds.gold.pi.asyncs.GetArrayAsyncTask.doInBackground(GetArrayAsyncTask.java:111)
      at com.kds.gold.pi.asyncs.GetArrayAsyncTask.doInBackground(GetArrayAsyncTask.java:24)
  </code></pre>
  
  <p>This project is using the Jackson faster XML library.</p>
  
  <p><strong>Edit:</strong>
   After some modifications, I ran into one weird issue that after initializing str with " " instead of null (<code>this actually solved null pointer exception but led to functional issue</code>). When I add a breakpoint to that line(<code>return (Map) JsonUtils.stringObject(str, Map.class); //Error is here</code>) and debug, the app works fine. Whereas when I run it through normal mode it gives <code>Data format error!</code> Toast as defined in onPost execute method.</p>