0

My app is still crashing but now it is giving me FATAL EXCEPTION: AsyncTask #3. I have been looking at my code for hours and I cannot find out what would be the reason for it to be crashing. So I was just hoping if somebody can tell me why and what I can to fix it.

07-28 14:50:19.728: E/AndroidRuntime(776): FATAL EXCEPTION: AsyncTask #3
07-28 14:50:19.728: E/AndroidRuntime(776): java.lang.RuntimeException: An error occured while executing doInBackground()
07-28 14:50:19.728: E/AndroidRuntime(776):  at android.os.AsyncTask$3.done(AsyncTask.java:299)
07-28 14:50:19.728: E/AndroidRuntime(776):  at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
07-28 14:50:19.728: E/AndroidRuntime(776):  at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
07-28 14:50:19.728: E/AndroidRuntime(776):  at java.util.concurrent.FutureTask.run(FutureTask.java:239)
07-28 14:50:19.728: E/AndroidRuntime(776):  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
07-28 14:50:19.728: E/AndroidRuntime(776):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
07-28 14:50:19.728: E/AndroidRuntime(776):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
07-28 14:50:19.728: E/AndroidRuntime(776):  at java.lang.Thread.run(Thread.java:856)
07-28 14:50:19.728: E/AndroidRuntime(776): Caused by: java.lang.NullPointerException
07-28 14:50:19.728: E/AndroidRuntime(776):  at com.theproblemsolver.ListView$LoadAllData.doInBackground(ListView.java:92)
07-28 14:50:19.728: E/AndroidRuntime(776):  at com.theproblemsolver.ListView$LoadAllData.doInBackground(ListView.java:1)
07-28 14:50:19.728: E/AndroidRuntime(776):  at android.os.AsyncTask$2.call(AsyncTask.java:287)
07-28 14:50:19.728: E/AndroidRuntime(776):  ... 4 more

MainActivity

public class MainActivity extends Activity {

    EditText et;
    Button getanswer;

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

        Button getanswer = (Button) findViewById(R.id.button1);
        getanswer.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent i = new Intent(MainActivity.this, ListView.class);
            startActivity(i);
        }


        });
    } 
}

ListView Activity

public class ListView extends ListActivity {

    private ProgressDialog pDialog;

    JSONParsser jParser = new JSONParsser();
    ArrayList<HashMap<String, String>> questionList;

    private static String URI = "http://example.com/json";;

    //JSONObject json = jParser.getJSONFromURI(URI); 

     final String TAG_RESULTS = "ResultsSet";
     final String TAG_SUBJECT = "Subject";
     final String TAG_NUMANSWERS = "NumAnswers";
     final String TAG_QUESTION = "Question";
     final String TAG_QUESTION_CONTENT = "Content";
     final String TAG_QUESTION_CHOSENANSWER = "ChosenAnswer";
     final String TAG_QUESTION_ANSWERS = "Answers";

            JSONArray ResultsSet = null;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); 
        //setContentView(R.layout.listview);

    questionList = new ArrayList<HashMap<String, String>>();

    android.widget.ListView lv = getListView(); 

    new LoadAllData().execute();

    lv.setOnItemClickListener(new OnItemClickListener() {

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


        }
    });

}

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == 100) {
            Intent intent = getIntent();
            startActivity(intent);
            finish();
        }
    }

    class LoadAllData extends AsyncTask<String, String, String> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(ListView.this);
            pDialog.setMessage("Loading Data. Please wait...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }

        protected String doInBackground(String... args) {

            try {
                    JSONObject json = jParser.getJSONFromURI(URI);
                    ResultsSet = json.getJSONArray(TAG_RESULTS);

                   for(int i = 0; i < ResultsSet.length(); i++) {
                       JSONObject r = ResultsSet.getJSONObject(i);
                       String Subject = r.getString(TAG_SUBJECT);
                       String NumAnswers = r.getString(TAG_NUMANSWERS);

                       JSONObject Question = r.getJSONObject(TAG_QUESTION);
                       String Content = Question.getString(TAG_QUESTION_CONTENT);
                       String ChosenAnswer = Question.getString(TAG_QUESTION_CHOSENANSWER);
                       String Answers = Question.getString(TAG_QUESTION_ANSWERS);

                       HashMap<String, String> map = new HashMap<String, String>();

                       map.put(TAG_SUBJECT, Subject);
                       map.put(TAG_NUMANSWERS, NumAnswers);

                       questionList.add(map);
                   }



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


        }


        protected void onPostExecute(String file_URI) {

            pDialog.closeOptionsMenu();
            runOnUiThread(new Runnable() {
            public void run() {
                ListAdapter adapter = new SimpleAdapter(getBaseContext(), questionList,
                        R.layout.row,
                        new String[] { TAG_SUBJECT, TAG_NUMANSWERS }, new int[] {
                        R.id.Subject, R.id.NumAnswers });

                setListAdapter(adapter);



                }
            });

        }
}}

JSONParsser class

public class JSONParsser {


    static InputStream is = null;
    static JSONObject jObj = null;
    static String json = "";
    EditText et;

    public JSONParsser () {
    }

    public JSONObject getJSONFromURI(String URI) {

        try{
        HttpClient client = new DefaultHttpClient();
        try {
            URI website = new URI("http://example.com/json");
            HttpGet request = new HttpGet();
            request.setURI(website);
            try {
                HttpResponse response = client.execute(request);
            HttpEntity httpEntity = response.getEntity();
            is = httpEntity.getContent();

            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } catch (URISyntaxException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        return jObj;

        }finally{}

    }{

    }

}

If you have any idea about why my code is crashing and other possible errors please let me know.

  • You're dereferencing a null pointer on line 92 in doInBackground(). I'm not going to count lines to figure out which one that is. Seems likely that either your JSON is missing data you expect it to have or you didn't get it at all. – Gabe Sechan Jul 28 '13 at 15:19
  • Line 92 is `for(int i = 0; i < ResultsSet.length(); i++) {` – Tommy Kind Jul 28 '13 at 15:26
  • So ResultsSet must be null. Which means TAG_RESULTS is not in your JSON from the service. – Gabe Sechan Jul 28 '13 at 15:37
  • ok, I will have to check but could line 39 be a problem too? `JSONArray ResultsSet = null;` – Tommy Kind Jul 28 '13 at 15:48

1 Answers1

0

In JSONParsser you return jObj which is null and not changed in method getJSONFromURI. The result of this method is used in LoadAllData and produces NPE.

esentsov
  • 6,372
  • 21
  • 28
  • Can you give me an example of how those two classes was suppose to look after changing variables? – Tommy Kind Jul 28 '13 at 16:02
  • I think you should parse response (check this: [link](http://stackoverflow.com/questions/9605913/how-to-parse-json-in-android)) in `JSONParsser`. And in `LoadAllData` you shoud check `json` variable for null value before using it. – esentsov Jul 28 '13 at 16:19