Android Java error handling XML file
Posted
by
Paul
on Stack Overflow
See other posts from Stack Overflow
or by Paul
Published on 2011-01-02T16:25:57Z
Indexed on
2011/01/02
16:54 UTC
Read the original article
Hit count: 141
I'm using SAX and XML reader to read XML weather info from the web and it works fine if the page exists. But if for instance the user inputs an invalid city, zip etc the XML page that gets read from is empty and the app force closes with nullpointerexception. The area that generates the error is here right at open inputstream. Any suggestions?:
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = null;
try {
sp = spf.newSAXParser();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/* Get the XMLReader of the SAXParser we created. */
XMLReader xr = null;
try {
xr = sp.getXMLReader();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/* Create a new ContentHandler and apply it to the XML-Reader*/
WeatherHandler myExampleHandler = new WeatherHandler();
xr.setContentHandler(myExampleHandler);
/* Parse the xml-data from our URL. */
try {
xr.parse(new InputSource(url.openStream()));
parsedWeatherDataSet =
myExampleHandler.getParsedData();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return parsedWeatherDataSet.toString();
© Stack Overflow or respective owner