java.lang.String org.json.JSONObject.getString(java.lang.String)' on a null object reference
<p>Im getting error in the following code. I am getting error in between the --->1 &amp; --->2 ..ie <strong>while</strong> loop. Got <strong>response code as 200</strong>.But still my app is crashed.</p>

<p>Note:
   The same question was already asked.Even I analyzed that but i m not getting the right thing.Please help me to fix it.</p>

<pre><code>if (responseCode == HttpURLConnection.HTTP_OK || responseCode == HttpURLConnection.HTTP_BAD_METHOD) {

    is = con.getInputStream();
    BufferedReader br = new BufferedReader(new InputStreamReader(is));
    String line;
    Log.e("LOGIN CHECK", "json"); // -----------&gt; 1 (Prints in Log.e)
    while ((line = br.readLine()) != null){
           buffer.append(line).append("\r\n");
    }
    json = buffer.toString();    // -----------&gt; 2 (Didnt print in Log.e)
    Log.e("LOGIN CHECK", "json" + buffer.toString());
    is.close();
 }
</code></pre>

<p><strong>LogCat Error:</strong></p>

<pre><code> java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String org.json.JSONObject.getString(java.lang.String)' on a null object reference
                                                       at com.xxxxx.restaurant.Fragment.SignUpFragment_1$ProcessLogin.onPostExecute(SignUpFragment_1.java:187)
                                                       at com.xxxx.restaurant.Fragment.SignUpFragment_1$ProcessLogin.onPostExecute(SignUpFragment_1.java:163)
                                                       at android.os.AsyncTask.finish(AsyncTask.java:632)
                                                       at android.os.AsyncTask.access$600(AsyncTask.java:177)
                                                       at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
                                                       at android.os.Handler.dispatchMessage(Handler.java:102)
                                                       at android.os.Looper.loop(Looper.java:145)
                                                       at android.app.ActivityThread.main(ActivityThread.java:5942)
                                                       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:1400)
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
</code></pre>

<p>Json PArser:</p>

<pre><code>public class JSONParser {

    static JSONObject jObj = null;
    static String json = "";

    public JSONParser() {
    }

    public JSONObject getJSONFromUrl(String url, HashMap&lt;String, String&gt; params) {
        HttpURLConnection con = null;
        InputStream is = null;

        try {
            con = (HttpURLConnection) (new URL(url)).openConnection();
            con.setRequestMethod("POST");
            con.connect();
            OutputStream os = con.getOutputStream();
            BufferedWriter writer = new BufferedWriter(
                    new OutputStreamWriter(os, "UTF-8"));
            writer.write(getPostDataString(params));
            writer.flush();
            writer.close();
            os.close();
            StringBuilder buffer = new StringBuilder();
            Log.e("Response Buffer", String.valueOf(buffer));
            int responseCode = con.getResponseCode();
            Log.e("Response Code", String.valueOf(responseCode));

            if (responseCode == HttpURLConnection.HTTP_OK || responseCode == HttpURLConnection.HTTP_BAD_METHOD) {

                is = con.getInputStream();
                BufferedReader br = new BufferedReader(new InputStreamReader(is));
                String line;
                Log.e("LOGIN CHECK", "json");
                while ((line = br.readLine()) != null)
                    buffer.append(line).append("\r\n");
                json = buffer.toString();
                Log.e("LOGIN CHECK", "json" + buffer.toString());
                is.close();
            }else{
                  json = "abc";
            }
            con.disconnect();
        } catch (Throwable t) {
            t.printStackTrace();
        } finally {
            try {
                if (is != null) {
                    is.close();
                }
            } catch (Throwable ignored) {
            }
            try {
                if (con != null) {
                    con.disconnect();
                }
            } catch (Throwable ignored) {
            }
        }
        try {
            jObj = new JSONObject(json);
        } catch (JSONException e) {
        }
        return jObj;
    }

    private String getPostDataString(HashMap&lt;String, String&gt; params) throws UnsupportedEncodingException {
        StringBuilder result = new StringBuilder();
        boolean first = true;
        for (Map.Entry&lt;String, String&gt; entry : params.entrySet()) {
            if (first)
                first = false;
            else
                result.append("&amp;");

            result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
            result.append("=");
            result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
        }

        return result.toString();
    }
}
</code></pre>

<p>SignUpFragment_1:</p>

<pre><code>try {
                if (json.getString(KEY_SUCCESS) != null) {  // Error in this LINE

                    String res = json.getString(KEY_SUCCESS);

                    if (Integer.parseInt(res) == 1) {
                        dbUtil.open();
                        JSONObject json_user = json.getJSONObject("user");

                        UserFunctions logout = new UserFunctions();
                        logout.logoutUser(context);

                        dbUtil.addLogIn(strUsername, strEmail, strPassword, LogInActivity.img);
                        Intent upanel = new Intent(context, MainActivity.class);
                        upanel.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        pDialog.dismiss();
                        startActivity(upanel);
                    } else {
                        pDialog.dismiss();
                        Toast.makeText(context, "Incorrect username/password", Toast.LENGTH_SHORT).show();
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
</code></pre>