0

Im still new with android and json please help me Im also getting Null Pointer Exception. What should I do? Thanks In Advance

Heres my code:

JSONArray ass=null;

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

//JSON
private class JSONParse extends AsyncTask<String, String, JSONObject> {
    private ProgressDialog pDialog;
    @Override
    protected void onPreExecute() {

    }

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

    @Override
    protected void onPostExecute(JSONObject json) {
        final ArrayList<String> ids = new ArrayList<String>();
        final ArrayList<String> assname = new ArrayList<String>();
        final ArrayList<String> duedate = new ArrayList<String>();
        final ArrayList<String> course = new ArrayList<String>();
        final ArrayList<String> note = new ArrayList<String>();
        try {
            ass = json.getJSONArray("ass");
            for(int i=0; i<ass.length(); i++){
                JSONObject student = ass.getJSONObject(i);
                ids.add(student.getString("id"));
                assname.add(student.getString("assname"));
                duedate.add(student.getString("duedate"));
                course.add(student.getString("course"));
                note.add(student.getString("note"));

            }
            final ListView listView = (ListView)findViewById(R.id.listView);
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(AssList.this,android.R.layout.simple_list_item_1, android.R.id.text1, course);
            // Assign adapter to ListView
            listView.setAdapter(adapter);
            // ListView Item Click Listener
            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    // ListView Clicked item value                       
                    String ass_id = ids.get(position);
                    String ass_name = assname.get(position);
                    String ass_date = duedate.get(position);
                    String ass_course = course.get(position);
                    String ass_note = note.get(position);
                    Intent i = new Intent(AssList.this, ViewAss.class);
                    i.putExtra("ass_id", ass_id);
                    i.putExtra("ass_name", ass_name);
                    i.putExtra("ass_date", ass_date);
                    i.putExtra("ass_course", ass_course);
                    i.putExtra("ass_note", ass_note);
                    startActivity(i);
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}

This the error I get:

07-20 19:20:35.635 30660-30711/com.example.shaishai.assignmenttrackertry E/JSON Parser: Error parsing data org.json.JSONException: Value [] of type org.json.JSONArray cannot be converted to JSONObject

12
  • what's your json string Commented Jul 20, 2016 at 12:04
  • Is it valid try jsonlint to validate it Commented Jul 20, 2016 at 12:04
  • Post your JsonResponse. Commented Jul 20, 2016 at 12:06
  • I think , ass.getJSONObject(i) is JsonArray Not JSONObject. Commented Jul 20, 2016 at 12:15
  • @GiteekaSawlani getJSONObject returns a JSONObject by definition. Commented Jul 20, 2016 at 12:17

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.