Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid asynctask exception
    text
    copied!<p>i'm doing an app that have to get an XML file from an url and parse it. I always find this log cat (PS:i can't find the correct google prettify sintax code highlighting): </p> <pre><code>11-06 23:12:44.940: E/Trace(8751): error opening trace file: No such file or directory(2) 11-06 23:12:51.345: E/Error:(8751): expected: /hr read: body (position:END_TAG &lt;/body&gt;@6:8 in java.io.StringReader@431887b8) 11-06 23:12:51.360: E/AndroidRuntime(8751): FATAL EXCEPTION: AsyncTask #1 11-06 23:12:51.360: E/AndroidRuntime(8751): java.lang.RuntimeException: An error occured while executing doInBackground() 11-06 23:12:51.360: E/AndroidRuntime(8751): at android.os.AsyncTask$3.done(AsyncTask.java:299) 11-06 23:12:51.360: E/AndroidRuntime(8751): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273) 11-06 23:12:51.360: E/AndroidRuntime(8751): at java.util.concurrent.FutureTask.setException(FutureTask.java:124) 11-06 23:12:51.360: E/AndroidRuntime(8751): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307) 11-06 23:12:51.360: E/AndroidRuntime(8751): at java.util.concurrent.FutureTask.run(FutureTask.java:137) 11-06 23:12:51.360: E/AndroidRuntime(8751): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) 11-06 23:12:51.360: E/AndroidRuntime(8751): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) 11-06 23:12:51.360: E/AndroidRuntime(8751): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569) 11-06 23:12:51.360: E/AndroidRuntime(8751): at java.lang.Thread.run(Thread.java:856) 11-06 23:12:51.360: E/AndroidRuntime(8751): Caused by: java.lang.NullPointerException 11-06 23:12:51.360: E/AndroidRuntime(8751): at com.ambro.app.Update$connection.doInBackground(Update.java:39) 11-06 23:12:51.360: E/AndroidRuntime(8751): at com.ambro.app.Update$connection.doInBackground(Update.java:1) 11-06 23:12:51.360: E/AndroidRuntime(8751): at android.os.AsyncTask$2.call(AsyncTask.java:287) 11-06 23:12:51.360: E/AndroidRuntime(8751): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305) 11-06 23:12:51.360: E/AndroidRuntime(8751): ... 5 more </code></pre> <p>the xml could be found at: <a href="http://www.lookedpath.tk/apps/firstapp/version.xml" rel="nofollow">http://www.lookedpath.tk/apps/firstapp/version.xml</a></p> <p>the update.java code: </p> <pre class="lang-java prettyprint-override"><code>public class Update extends Activity { private TextView testo2; @Override public void onCreate(Bundle savedInstanceState) { setContentView(R.layout.update); super.onCreate(savedInstanceState); testo2= (TextView) findViewById(R.id.textView2); } public void goToUpdate (View view) { System.out.println("ciao"); new connection().execute(); } public class connection extends AsyncTask&lt;Void, Void, Boolean&gt; { protected Boolean doInBackground(Void... params) { boolean updated=false; String lastversion=null; Element e=null; final String URL = "http://www.lookedpath.tk/apps/firstapp/version.xml"; final String VERSION = "version"; final String APPLICATION = "application"; XMLParser parser = new XMLParser(); String xml = parser.getXmlFromUrl(URL); // getting XML Document doc = parser.getDomElement(xml); // getting DOM element NodeList nl = doc.getElementsByTagName(APPLICATION); for (int i=0;i&lt;nl.getLength();i++) { e = (Element) nl.item(0); lastversion = parser.getValue(e, VERSION); // name child value } String actver = getString(R.string.version); if(actver==lastversion) updated=true; return updated; } protected void onPostExecute(Boolean... result) { if(result[0]==false){ testo2.setText(R.string.newversion); } else { testo2.setText(R.string.nonewversion); } } }; } </code></pre> <p>so this is the xmlparser.java file:</p> <pre class="lang-java prettyprint-override"><code>public class XMLParser { public String getXmlFromUrl(String url) { String xml = null; try { // defaultHttpClient DefaultHttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(url); HttpResponse httpResponse = httpClient.execute(httpPost); HttpEntity httpEntity = httpResponse.getEntity(); xml = EntityUtils.toString(httpEntity); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } // return XML return xml; } public Document getDomElement(String xml){ Document doc = null; DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); try { DocumentBuilder db = dbf.newDocumentBuilder(); InputSource is = new InputSource(); is.setCharacterStream(new StringReader(xml)); doc = db.parse(is); } catch (ParserConfigurationException e) { Log.e("Error: ", e.getMessage()); return null; } catch (SAXException e) { Log.e("Error: ", e.getMessage()); return null; } catch (IOException e) { Log.e("Error: ", e.getMessage()); return null; } // return DOM return doc; } public String getValue(Element item, String str) { NodeList n = item.getElementsByTagName(str); return this.getElementValue(n.item(0)); } public final String getElementValue( Node elem ) { Node child; if( elem != null){ if (elem.hasChildNodes()){ for( child = elem.getFirstChild(); child != null; child = child.getNextSibling() ){ if( child.getNodeType() == Node.TEXT_NODE ){ return child.getNodeValue(); } } } } return ""; } } </code></pre> <p>the logcat say that the error is in the line 39 of the update class:</p> <pre class="lang-java prettyprint-override"><code>NodeList nl = doc.getElementsByTagName(APPLICATION); </code></pre> <p>but i can't manage to resolve the problem. can anyone help me?</p> <p><strong>UPDATE:</strong> i have fixed the error 405 but i still have a null pointer exception. i found that the program run correctly but never enter in the post execute method because of this exception..</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload