Testing For a null returned value
<p>In my app, I am parsing XML files - </p>

<pre><code>        Document doc = XMLfunctions.XMLfromString(xml);
</code></pre>

<p>using the following function</p>

<pre><code>public final static Document XMLfromString(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) {
        System.out.println("XML parse error: " + e.getMessage());
        return null;
    } catch (SAXException e) {
        System.out.println("Wrong XML file structure: " + e.getMessage());
         return null;
    } catch (IOException e) {
        System.out.println("I/O exeption: " + e.getMessage());
        return null;
    }

    return doc;
}
</code></pre>

<p>If everything works well than the program checks for the number of results.  My problem comes in when I run into a badly formed xml file. Such as when a WordPress file has the  tag inside.  What happens is my app crashes and when I look at the log the program has caught the exception</p>

<pre><code>   } catch (SAXException e) {
System.out.println("Wrong XML file structure: " + e.getMessage());
         return null;
</code></pre>

<p>which is fine.  Now what I want to do is test for null and then call the finish() to go back to the menu, rather than face a forced close.</p>

<p>I've used</p>

<pre><code>if (null==doc){
    Toast.makeText(this, "Sorry The XML File " + target +" is Misformed", Toast.LENGTH_SHORT).show();
       finish();
   }
</code></pre>

<p>and</p>

<pre><code>if (doc==null){
        Toast.makeText(this, "Sorry The XML File " + target +" is Misformed", Toast.LENGTH_SHORT).show();
           finish();
        }
</code></pre>

<p>to no avail. What would be the best way to handle these types of situations?</p>

<p>Still searching
I tried this</p>

<pre><code>if(XMLfunctions.XMLfromString(xml)==null){
    Toast.makeText(this, "Badly Formed File", Toast.LENGTH_LONG).show();  
    finish();
</code></pre>

<p>}        </p>

<p>Same issues - the line of code after the call is</p>

<pre><code>int numResults = XMLfunctions.numResults(doc);
</code></pre>

<p>The function code itself is</p>

<pre><code>public static int numResults(Document doc){
        int res = -1;
        Node results = doc.getDocumentElement();
        try{
        res = Integer.valueOf(results.getAttributes().getNamedItem("count").getNodeValue());
        }catch(Exception e){
            res = -1;
        }
        return res;
    }
</code></pre>

<p>The initial exception message being returned is message that is being returned for the function getXML is</p>

<blockquote>
  <p>03-27 12:46:26.917: I/System.out(10311): Wrong XML file structure: name expected >(position:START_TAG @29:27 in java.io.StringReader@405a3d30) </p>
</blockquote>

<p>At that point it starts throwing fatal exceptions</p>
