jsonObject Null Pointer Exception when clicking the display page
<p>Help jsonObject NPE. I can parse and display the results i want to get from my db. I tried the php code in jsonlint.com it works. My problem is in android studio, when i parse and display the result it works. But when i run the activity on where it is supposed to display the app crashes.</p>

<p>Parser</p>

<pre><code>public class Send extends Activity {
EditText et1, et2, et3, et4, et5;
String team1, score1, team2, score2, Type;
Button logout;
String json_string, JSON_STRING;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.send);

    et1 = (EditText) findViewById(R.id.et1);
    et2 = (EditText) findViewById(R.id.et2);
    et3 = (EditText) findViewById(R.id.et3);
    et4 = (EditText) findViewById(R.id.et4);
    et5 = (EditText) findViewById(R.id.et5);
    logout = (Button) findViewById(R.id.logout);


    logout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(v.getContext(), login.class));
            finish();
        }
    });
}

public void send(View view) {
    Type = et1.getText().toString();
    team1 = et2.getText().toString();
    score1 = et3.getText().toString();
    team2 = et4.getText().toString();
    score2 = et5.getText().toString();
    String method = "send";
    BackgroundTask backgroundTask = new BackgroundTask(this);
    backgroundTask.execute(method, Type, team1, score1, team2, score2);

}


public void get(View view) {
    new BgTask().execute();
}

class BgTask extends AsyncTask&lt;Void, Void, String&gt; {
    String json_url;

    @Override
    protected void onPreExecute() {
        json_url = "http://10.0.2.2/result/hehe.php";
    }

    @Override
    protected String doInBackground(Void... params) {
        try {
            URL url = new URL(json_url);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            InputStream inputstream = httpURLConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputstream));
            StringBuilder stringBuilder = new StringBuilder();
            while ((JSON_STRING = bufferedReader.readLine()) != null) {
                stringBuilder.append(JSON_STRING + "\n");
            }

            bufferedReader.close();
            inputstream.close();
            httpURLConnection.disconnect();
            return stringBuilder.toString().trim();

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
    }

    @Override
    protected void onPostExecute(String result) {
        json_string = result;
    }
}

public void publishGame(View view)
{
    if(json_string == null)
    {
        Toast.makeText(getApplicationContext(), "Get Data First",Toast.LENGTH_SHORT).show();
    }
    else
    {
        Intent intent = new Intent(this, Games.class);
        intent.putExtra("json_data", json_string);
        startActivity(intent);
    }
}
</code></pre>

<p>}</p>

<p>Here is where the app display the json result</p>

<pre><code> @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.games);
    final Spinner sports;
    final String[] str = {"Sports", "Basketball", "Football", "Futsal", "Softball", "Badminton", "Track and Field", "Chess", "Table Tennis", "Swimming", "Volleyball", "Sepak Takraw", "Taekwondo", "Judo", "Frisbee"};

    sports = (Spinner) findViewById(R.id.sp1);
    nonsports = (Spinner) findViewById(R.id.sp2);
    games = (Button) findViewById(R.id.games);
    news = (Button) findViewById(R.id.news);
    sched = (Button) findViewById(R.id.sched);
    stand = (Button) findViewById(R.id.stand);

    listview = (ListView) findViewById(R.id.listView);
    gamesAdapter = new GamesAdapter(this,R.layout.gamesadapterlayout);
    listview.setAdapter(gamesAdapter);
    Bundle intent=getIntent().getExtras();
    if(intent !=null) {

        json_string = getIntent().getExtras().getString("json_data");
    }


    ArrayAdapter&lt;String&gt; sp1 = new ArrayAdapter&lt;String&gt;(this, R.layout.support_simple_spinner_dropdown_item, str);
    sports.setAdapter(sp1);


    games.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
             Intent intent = new Intent(v.getContext(), Games.class);   startActivity(getIntent());
        }
    });

    news.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(v.getContext(), MainActivity.class);
            startActivity(intent);
        }
    });

    stand.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(v.getContext(), Standings.class);
            startActivity(intent);
        }
    });

    sched.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(v.getContext(), Schedule.class);
            startActivity(intent);
        }
    });

    sports.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView&lt;?&gt; parent, View view, int position, long row_id) {
            Intent intent = null;




        }

        @Override
        public void onNothingSelected(AdapterView&lt;?&gt; parent) {

        }
    });


    try {
            jsonObject = new JSONObject(json_string);
            jsonArray = jsonObject.getJSONArray("server_response");
            int count = 0;
            String team1, score1, team2, score2, Type;

            while (count &lt; jsonArray.length()) {
                JSONObject JO = jsonArray.getJSONObject(count);
                team1 = JO.getString("team1");
                score1 = JO.getString("score1");
                team2 = JO.getString("team2");
                score2 = JO.getString("score2");
                Type = JO.getString("Type");
                Downloader downloader = new Downloader(team1, score1, team2, score2, Type);
                gamesAdapter.add(downloader);

                count++;
            }


    } catch (JSONException e) {
        e.printStackTrace();
    }

}
</code></pre>

<p>The error is pointing at </p>

<pre><code>try {
    jsonObject = new JSONObject(json_string); 
</code></pre>

<p>and it is telling me that it is NPE</p>

<p><a href="https://i.stack.imgur.com/XrP1j.jpg" rel="nofollow">Here is an image where i will dispaly the result in Games.class</a></p>

<p><a href="https://i.stack.imgur.com/aWYpD.jpg" rel="nofollow">The result of the display in the Games.class</a></p>

<p>Now the problem is after displaying it. When i click other menu and click the Games menu again it crashes. Because of the jsonObject NPE. Thanks</p>

<p>Logcat</p>

<pre><code>E/AndroidRuntime: FATAL EXCEPTION: main
                                                                        Process: com.example.abe.ateneofiesta, PID: 6392
                                                                        java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.abe.ateneofiesta/com.example.abe.ateneofiesta.Games}: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
                                                                            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                                            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                            at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                            at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                            at android.os.Looper.loop(Looper.java:148)
                                                                            at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                         Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
                                                                            at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116)
                                                                            at org.json.JSONTokener.nextValue(JSONTokener.java:94)
                                                                            at org.json.JSONObject.&lt;init&gt;(JSONObject.java:156)
                                                                            at org.json.JSONObject.&lt;init&gt;(JSONObject.java:173)
                                                                            at com.example.abe.ateneofiesta.Games.onCreate(Games.java:111)
                                                                            at android.app.Activity.performCreate(Activity.java:6237)
                                                                            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                                            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                            at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                            at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                            at android.os.Looper.loop(Looper.java:148) 
                                                                            at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                            at java.lang.reflect.Method.invoke(Native Method) 
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
</code></pre>