jsonObject Null指针单击显示页面时出现异常

时间:2016-10-27 20:23:27

标签: android android-asynctask nullpointerexception jsonobject

帮助jsonObject NPE。我可以解析并显示我想从我的数据库中获得的结果。我尝试了jsonlint.com中的php代码。我的问题是在android studio中,当我解析并显示它的工作结果时。但是当我在应用程序崩溃的地方运行活动时。

分析器

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<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);
    }
}

}

这是应用程序显示json结果的位置

 @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<String> sp1 = new ArrayAdapter<String>(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<?> parent, View view, int position, long row_id) {
            Intent intent = null;




        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });


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

}

错误指向

try {
    jsonObject = new JSONObject(json_string); 

它告诉我它是NPE

Here is an image where i will dispaly the result in Games.class

The result of the display in the Games.class

现在问题是在显示之后。当我点击其他菜单并再次单击“游戏”菜单时,它会崩溃。因为jsonObject NPE。感谢

logcat的

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.<init>(JSONObject.java:156)
                                                                            at org.json.JSONObject.<init>(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) 

0 个答案:

没有答案