0 Replies - 93 Views - Last Post: 30 July 2013 - 04:18 AM

#1 bobby5678  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 29-July 13

Android FATAL EXECEPTION: AsyncTask #5 error

Posted 30 July 2013 - 04:18 AM

I have been working on my Android application and I have ran into a roble, that is driving me nuts. The error I have is in the title and I really do not know what could be causing this error to force close my app. I really do not know what else I can do to fix this problem. So I was just hoping if somebody can take a look at it and tell me what could be causing this problem and what I can do to fix it.

Here is my LogCat:

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


ListView Activity:

public class ListView extends ListActivity {
	
	public static EditText et;

	private ProgressDialog pDialog;

	JSONParsser jParser = new JSONParsser();
	ArrayList<HashMap<String, String>> questionList;
	
	
	
	
	private static String URL = "http://example.com/json";

	//JSONObject json = jParser.getJSONFromURI(URI);
	 
	 final String TAG_QUESTION_SUBJECT = "Subject";
	 final String TAG_QUESTION_NUMANSWERS = "NumAnswers";
	 final String TAG_QUESTION = "Question";
	 final String TAG_QUESTION_CONTENT = "Content";
	 final String TAG_QUESTION_CHOSENANSWER = "ChosenAnswer";
	 final String TAG_ANSWERS = "Answers";
	 final String TAG_ANSWER = "Answer";	
			
	 		JSONArray Question;
	
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); 
        //setContentView(R.layout.listview);
    			
	//questionList = new ArrayList<HashMap<String, String>>();

 
    
    new LoadAllData().execute();

        }

	@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) {
	    			
	    	JSONObject jObj = jParser.getJSONFromURL(URL);
	    	
	    	try {
	        	    
	                Question = jObj.getJSONArray(TAG_QUESTION);

	               for(int i = 0; i < Question.length(); i++) {
	                   JSONObject q = Question.getJSONObject(i);
	                  
	                   String Subject = q.getString(TAG_QUESTION_SUBJECT);
	                   String NumAnswers = q.getString(TAG_QUESTION_NUMANSWERS);
	                   String ChosenAnswers = q.getString(TAG_QUESTION_CHOSENANSWER);
	                   String Content = q.getString(TAG_QUESTION_CONTENT);
	                   
	                   JSONObject Answers = q.getJSONObject(TAG_ANSWERS);
	                   String Answer = Answers.getString(TAG_ANSWER);
	                   
	                 questionList = new ArrayList<HashMap<String, String>>();
	                   
	                   HashMap<String, String> map = new HashMap<String, String>();

	                   map.put(TAG_QUESTION_SUBJECT, Subject);
	                   map.put(TAG_QUESTION_NUMANSWERS, NumAnswers);
	                   
	                   questionList.add(map);
	               
	                   
	               }



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


	    }

	    @Override
	    protected void onPostExecute(String file_URL) {
 
	            ListAdapter adapter = new SimpleAdapter(getBaseContext(), questionList,
	                    R.layout.listview,
	                    new String[] { TAG_QUESTION_SUBJECT, TAG_QUESTION_NUMANSWERS }, new int[] {
	                    R.id.Subject, R.id.NumAnswers });
	           
	            setListAdapter(adapter);
	            
	            //if (pDialog != null && pDialog.isShowing()) pDialog.dismiss();
	            
	            android.widget.ListView lv = getListView();
	            
	            lv.setOnItemClickListener(new OnItemClickListener() {

					@Override
					public void onItemClick(AdapterView<?> arg0, View arg1,int arg2, long arg3) {
						// TODO Auto-generated method stub
						
					}

				
						
					
	            });
	            
	    	}	    
}}


JSONParsser Activity

public class JSONParsser {

	static InputStream is;
    static JSONObject jObj;
    static String json = "";
	public EditText et;
    
	public JSONParsser () {
    }

	public JSONObject getJSONFromURL(String URL) {
		
		try{
  		HttpClient client = new DefaultHttpClient();
  		//URL website = new URL("http://example.com/json");
		HttpGet request = new HttpGet(URL);
		//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();
		}
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            json = sb.toString();
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }
 
        // try parse the string to a JSON object
        try {
            jObj = new JSONObject(json);
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }
  		
  		return jObj;
		
		}finally{}
	  
	}{

	}}


Is This A Good Question/Topic? 0
  • +

Page 1 of 1