Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jul 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Android AsyncTask #1 error

    Hello, I have a quite a bit of a problem on my hands. I have an android application that I have been working on for weeks and I can not figure this problem so I was hoping if somebody can help. The problem I have is an FATAL EXCEPTION: AsyncTask #1 error and it force closing my application. So can somebody please why is this occurring and what I can do to fix this.

    Here is my LogCat:
    Code:
    07-31 18:35:11.379: E/AndroidRuntime(2503): FATAL EXCEPTION: AsyncTask #1
    07-31 18:35:11.379: E/AndroidRuntime(2503): java.lang.RuntimeException: An error occured while executing doInBackground()
    07-31 18:35:11.379: E/AndroidRuntime(2503): 	at android.os.AsyncTask$3.done(AsyncTask.java:299)
    07-31 18:35:11.379: E/AndroidRuntime(2503): 	at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
    07-31 18:35:11.379: E/AndroidRuntime(2503): 	at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
    07-31 18:35:11.379: E/AndroidRuntime(2503): 	at java.util.concurrent.FutureTask.run(FutureTask.java:239)
    07-31 18:35:11.379: E/AndroidRuntime(2503): 	at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
    07-31 18:35:11.379: E/AndroidRuntime(2503): 	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
    07-31 18:35:11.379: E/AndroidRuntime(2503): 	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
    07-31 18:35:11.379: E/AndroidRuntime(2503): 	at java.lang.Thread.run(Thread.java:856)
    07-31 18:35:11.379: E/AndroidRuntime(2503): Caused by: java.lang.NullPointerException
    07-31 18:35:11.379: E/AndroidRuntime(2503): 	at com.theproblemsolver.ListView$LoadAllData.doInBackground(ListView.java:95)
    07-31 18:35:11.379: E/AndroidRuntime(2503): 	at com.theproblemsolver.ListView$LoadAllData.doInBackground(ListView.java:1)
    07-31 18:35:11.379: E/AndroidRuntime(2503): 	at android.os.AsyncTask$2.call(AsyncTask.java:287)
    07-31 18:35:11.379: E/AndroidRuntime(2503): 	at java.util.concurrent.FutureTask.run(FutureTask.java:234)
    07-31 18:35:11.379: E/AndroidRuntime(2503): 	... 4 more
    ListView Class:
    Code:
    public class ListView extends ListActivity {
    	
    	public static EditText et;
    
    	private ProgressDialog pDialog;
    
    	JSONParsser jParser = new JSONParsser();
    	ArrayList<HashMap<String, String>> questionList;
    	
    	
    	
    	
    	public static String URL = "http://example.com/json";
    
    	//JSONObject json = jParser.getJSONFromURI(URI);
    	 
    	 final String TAG_RESULTS = "results";
    	 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";	
    	 final String TAG_ANSWERS_CONTENT = "Content";		
    	 		
    	 		JSONArray results = null;
    	
    	
    	@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();
    	        if (pDialog != null && pDialog.isShowing()) pDialog.dismiss();
    		}
    
    	    protected String doInBackground(String... args) {
    	    			
    	    	JSONObject jObj = jParser.getJSONFromURL(URL);
    	    	
    	    	try {
    	        	    
    	    		results = jObj.getJSONArray(TAG_RESULTS);
    
    	               for(int i = 0; i < results.length(); i++) {
    	                   JSONObject r = results.getJSONObject(i);
    	                  
    	                   JSONObject Question = r.getJSONObject(TAG_QUESTION);
    	                   String Subject = Question.getString(TAG_QUESTION_SUBJECT);
    	                   String NumAnswers = Question.getString(TAG_QUESTION_NUMANSWERS);
    	                   String ChosenAnswers= Question.getString(TAG_QUESTION_CHOSENANSWER);
    	                   String Content = Question.getString(TAG_QUESTION_CONTENT);
    	                   
    	                   JSONObject Answers = Question.getJSONObject(TAG_ANSWERS);
    	                   JSONObject Answer = Answers.getJSONObject(TAG_ANSWER);
    	                   String Content1 = Answers.getString(TAG_ANSWERS_CONTENT);
    	                   
    	                 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_RESULTS ;
    
    	    }
    
    	    @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);
    	            
    	            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
    						
    					}
    
    				
    						
    					
    	            });
    	            
    	    	}	    
    }}
    my JSONParsser Class that contains my getJSONFromURL method
    Code:
    public class JSONParsser {
    
    	static InputStream is = null;
        static JSONObject jObj = null;
        static String json = "";
    	public EditText et;
        
    	public JSONParsser () {
        }
    
    	public JSONObject getJSONFromURL(String URL) {
    		
    		try{
      		HttpClient client = new DefaultHttpClient();
    		HttpPost request = new HttpPost(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 {
                //jObj = new JSONObject(json);
            //} catch (JSONException e) {
                //Log.e("JSON Parser", "Error parsing data " + e.toString());
            //}
      		
      		return jObj;
    		
    		}finally{}
    	  
    	}{
    
    	}}

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Java != JavaScript
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #3
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    It's always a good idea to actually utilize the stacktrace and read a little further down.

    07-31 18:35:11.379: E/AndroidRuntime(2503): Caused by: java.lang.NullPointerException
    07-31 18:35:11.379: E/AndroidRuntime(2503): at com.theproblemsolver.ListView$LoadAllData.doInBackground(ListView.java:95)
    What more than the exact Exception and line number do you want?


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •