NullPointerException getJSONArray

时间:2015-05-03 09:33:05

标签: java android json

您好我在解析此代码的onPostExecute部分时遇到问题,我想我做得对,但问题仍然存在。它说我在postExecute()方法行74和46上得到了一个nullPointerException。有人可以帮我解决这个问题。

这是错误日志

E/AndroidRuntime﹕ FATAL EXCEPTION: main
        Process: com.example.crow.androidappthirteen, PID: 25314
        java.lang.NullPointerException
                at com.example.crow.androidappthirteen.MainActivity$JSONParse.onPostExecute(MainActivity.java:74)
                at com.example.crow.androidappthirteen.MainActivity$JSONParse.onPostExecute(MainActivity.java:46)
                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:157)
                at android.app.ActivityThread.main(ActivityThread.java:5335)
                at java.lang.reflect.Method.invokeNative(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:515)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
                at dalvik.system.NativeStart.main(Native Method)

这是java文件:

public class MainActivity extends ActionBarActivity {
    String id, name, course;
    JSONArray students = null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        new JSONParse().execute();
    }

    @Override
    protected void onResume() {
// TODO Auto‐generated method stub
        super.onResume();
        new JSONParse().execute();
    }

    //JSON
    /*THIS IS LINE 46*/        private class JSONParse extends     AsyncTask<String, String, JSONObject> {
        private ProgressDialog pDialog;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(MainActivity.this);
            pDialog.setMessage("Getting Data ...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }

        @Override
        protected JSONObject doInBackground(String... args) {
            JSONParser jParser = new JSONParser();
// Getting JSON from URL
            JSONObject json = jParser.getJSONFromUrl("http://192.168.43.43/mymobile/getstudentlist.php");
            return json;
        }

        @Override
        protected void onPostExecute(JSONObject json) {
            pDialog.dismiss();
            final ArrayList<String> ids = new ArrayList<String>();
            final ArrayList<String> names = new ArrayList<String>();
            final ArrayList<String> courses = new ArrayList<String>();
            try {
 /*   THIS IS LINE 74   */    students = json.getJSONArray("students");
                for (int i = 0; i < students.length(); i++) {
                    JSONObject student = students.getJSONObject(i);
                    ids.add(student.getString("id"));
                    names.add(student.getString("name"));
                    courses.add(student.getString("course"));
                }
                final ListView listView = (ListView) findViewById(R.id.list);
                ArrayAdapter<String> adapter = new
                        ArrayAdapter<String>(MainActivity.this,
                        android.R.layout.simple_list_item_1, android.R.id.text1,
                        names);

// Assign adapter to ListView
                listView.setAdapter(adapter);
// ListView Item Click Listener
                listView.setOnItemClickListener(new OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> parent, View view,
                                            int position, long id) {

// ListView Clicked item value
                        String stud_id = ids.get(position);
                        String stud_name = names.get(position);
                        String stud_course = courses.get(position);

                        Intent i = new Intent(MainActivity.this, ActivityEditDel.class);
                        i.putExtra("stud_id", stud_id);
                        i.putExtra("stud_name", stud_name);
                        i.putExtra("stud_course", stud_course);
                        startActivity(i);
                    }
                });

            } catch (
                    JSONException e
                    )

            {
                e.printStackTrace();
            }
        }


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

这是JSON试图访问的php文件。

<?php
    $database_name = 'dbstudrec';
    $con = mysql_connect('localhost', 'root') or die(mysql_error());
    mysql_select_db($database_name) or die(msql_error());

    $query = "SELECT * FROM tblstudent";
    $result = mysql_query($query);
    $rec = array();

    while($row = mysql_fetch_assoc($result)){
        $rec['students'][] = $row;

    }
    echo json_encode($rec);
    mysql_close($con);

?>

非常感谢任何帮助。谢谢。

更新 我不知道为什么我在这些行上得到null但是当我打开getstudentlist.php文件时,我得到一个输出。所以必须有另一个原因让它变为空。

0 个答案:

没有答案