android jsonObject Null Pointer Exception [duplica

2019-09-13 11:34发布

This question already has an answer here:

I am new to JSON. I need help. My android studio keeps on telling me that my jsonobject is NULL. I can parse and display my jsonarray into a listview. But when i click the page where i displayed it, my app crashes.

Parser

class BgTask extends AsyncTask<Void, Void, String> {
    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 that posts

Bundle intent=getIntent().getExtras();
    if(intent !=null) {
        json_string = intent.getString("json_data");

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

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

            while (count < 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();
    }

}

The error pointing is here try { jsonObject = new JSONObject(json_string);

1条回答
放我归山
2楼-- · 2019-09-13 12:20

The error is because you are trying to parse "json_data" to JSONObject which is not a valid json. Perhaps you are having server response in a variable called json_data which is of String type if that is the case you need to pass that variable instead of passing String literal into JSONObject constructor.

查看更多
登录 后发表回答