title: Asynctask and parsing JSONObject from url NullPointerException
body: <p>Here's my problem.</p>
  
  <p>I've tried to get a <code>JSONObject</code> from an <code>url</code>. Here's my parser :</p>
  
  <pre><code>public class JSONParser {
      static InputStream is = null;
      static JSONObject jObj = null;
      static String json = "";
      // constructor
      public JSONParser() {
      }
      public JSONObject getJSONFromUrl(String url) {
          // Making HTTP request
          try {
              // defaultHttpClient
              HttpParams params = new BasicHttpParams();
              params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
              HttpClient httpClient = new DefaultHttpClient(params);
              HttpPost httpPost = new HttpPost(url);
              HttpResponse httpResponse = httpClient.execute(httpPost);
              HttpEntity httpEntity = httpResponse.getEntity();
              is = httpEntity.getContent();
          } catch (UnsupportedEncodingException e) {
              e.printStackTrace();
          } catch (ClientProtocolException e) {
              e.printStackTrace();
          } catch (IOException e) {
              e.printStackTrace();
          }
          try {
              BufferedReader reader = new BufferedReader(new InputStreamReader(
                      is, "iso-8859-1"), 8);
              StringBuilder sb = new StringBuilder();
              String line = null;
              while ((line = reader.readLine()) != null) {
                  sb.append(line + "\n");
              }
              is.close();
              json = sb.toString();
          } catch (Exception e) {
              Log.e("Buffer Error", "Error converting result " + e.toString());
          }
          // try parse the string to a JSON object
          try {
              jObj = new JSONObject(json);
          } catch (JSONException e) {
              Log.e("JSON Parser", "Error parsing data " + e.toString());
          }
          // return JSON String
          return jObj;
      }
  }
  </code></pre>
  
  <p>Here's my main activity :</p>
  
  <pre><code>public class MainActivity extends ActionBarActivity {
  
      private TextView tv;
      private static final String urllisteemplois = "the url that contains the json";
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          tv = (TextView) findViewById(R.id.text);
          JParse jParse = new JParse(urllisteemplois);
          jParse.execute();
          String result = jParse.concat();
          tv.setText(result);
      }
  }
  </code></pre>
  
  <p>And here's my ASynctask :</p>
  
  <pre><code>public class JParse extends AsyncTask&lt;String, String, JSONObject&gt; {
      private String url;
      private JSONObject json;
  
      public JParse(String url) {
          this.url=url;
      }
  
  
      @Override
      protected void onPreExecute() {
          super.onPreExecute();
      }
  
      @Override
      protected JSONObject doInBackground(String... args) {
          JSONParser jParser = new JSONParser();
          JSONObject json2 = jParser.getJSONFromUrl(url);
          return json2;
      }
  
      @Override
      protected void onPostExecute(JSONObject json2) {
          this.json=json2;
      }
  
      public String concat()
      {
          String result = this.json.toString()+"hello";
          return result;
      }
  }
  </code></pre>
  
  <p>I get this error :</p>
  
  <pre><code>01-20 03:18:43.797    2298-2298/com.aziz.bdk.ryadencule E/AndroidRuntime﹕ FATAL EXCEPTION: main
  Process: com.aziz.bdk.ryadencule, PID: 2298
  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.aziz.bdk.ryadencule/com.aziz.bdk.ryadencule.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String org.json.JSONObject.toString()' on a null object reference
  </code></pre>
  
  <p>I tried some stuff and I know that in the <code>onPostExecute</code>, <code>this.json</code> is not <code>null</code> and contains my parsed <code>JSON</code>, in the <code>concat()</code> method it is <code>null</code>.</p>
  
  <p>Also, I need to add <code>onPostExecute(json2)</code> in <code>doInBackground(..)</code>, it won't do it automatically.</p>
  
  <p>Thanks for the help !</p>