Solved

Easy Question: Missing Return Statement

Posted on 2006-07-22
6
311 Views
Last Modified: 2010-04-17
The following code gives me the following parse error, and I can't figure it out for the life of me.  I'm new to java and it's probably something amazingly simple.

-------------------------------------------------

import java.io.File;
import org.w3c.dom.Document;
import org.w3c.dom.*;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

public class XMLDatabase {

      public static void main (String args[]) {

            String file = "personas.xml";
            String rowNodeId = "persona";
            //String file = args[0];

            //callFile(file);
            numRows(file, rowNodeId);

      }

      static Document callFile(String inFile) {

            try {

                  DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
                  DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
                  Document doc = docBuilder.parse (new File(inFile));

                  return doc;

            } catch (SAXParseException err) {
                  System.out.println ("** Parsing error" + ", line "
                         + err.getLineNumber () + ", uri " + err.getSystemId ());
                  System.out.println(" " + err.getMessage ());

            } catch (SAXException e) {
                  Exception x = e.getException ();
                  ((x == null) ? e : x).printStackTrace ();

            } catch (Throwable t) {
                  t.printStackTrace ();
            }

      }

      static void numRows(String inFile, String inRowNodeId) {

            Document doc = callFile(inFile);

            NodeList rowArray = doc.getElementsByTagName(inRowNodeId);
            int totalRows = rowArray.getLength();

            System.out.println(totalRows);

      }
}

-------------------------------------------------
0
Comment
Question by:jtgraphic
6 Comments
 

Author Comment

by:jtgraphic
ID: 17162003
C:\My Documents\Code Library\Java\XMLDatabase.java:46: missing return statement
      }
        ^
1 error

is the error
0
 

Assisted Solution

by:NeoPhreak
NeoPhreak earned 25 total points
ID: 17162230
You need to have an additional return statement either (a) in each catch segment or (b) after the whole try/catch block.  If the exception happens before the program reaches 'return doc' the function now has no returned value that it requires.


Try something like:

static Document callFile(String inFile) {

          try {

               DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
               DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
               Document doc = docBuilder.parse (new File(inFile));

               return doc;

          } catch (SAXParseException err) {
               System.out.println ("** Parsing error" + ", line "
                     + err.getLineNumber () + ", uri " + err.getSystemId ());
               System.out.println(" " + err.getMessage ());

          } catch (SAXException e) {
               Exception x = e.getException ();
               ((x == null) ? e : x).printStackTrace ();

          } catch (Throwable t) {
               t.printStackTrace ();
          }

          //Return false if code makes it this far because the document variable didn't get set properly
          return false;
     }

 That should get rid of the error!
0
 
LVL 6

Assisted Solution

by:MuhammadAdil
MuhammadAdil earned 25 total points
ID: 17162646
Hi Dear

Place return statment in also catch blocks.

static Document callFile(String inFile) {

            Document doc;
          try {

               DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
               DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
              doc = docBuilder.parse (new File(inFile));

               return doc;

          } catch (SAXParseException err) {
               System.out.println ("** Parsing error" + ", line "
                     + err.getLineNumber () + ", uri " + err.getSystemId ());
               System.out.println(" " + err.getMessage ());
               return doc;

          } catch (SAXException e) {
               Exception x = e.getException ();
               ((x == null) ? e : x).printStackTrace ();
                return doc;

          } catch (Throwable t) {
               t.printStackTrace ();
                return doc;
          }

     }
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 25

Accepted Solution

by:
InteractiveMind earned 450 total points
ID: 17162703
The printStackTace() method calls will end execution, so there's no need to add a return statement directly after them.

NeoPhreak, you cannot return 'false' when the return type is an object.
Try null instead:


static Document callFile(String inFile) {

          try {

               DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
               DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
               Document doc = docBuilder.parse (new File(inFile));

               return doc;

          } catch (SAXParseException err) {
               System.out.println ("** Parsing error" + ", line "
                     + err.getLineNumber () + ", uri " + err.getSystemId ());
               System.out.println(" " + err.getMessage ());
          } catch (SAXException e) {
               Exception x = e.getException ();
               ((x == null) ? e : x).printStackTrace ();
          } catch (Throwable t) {
               t.printStackTrace ();
          }

          return null;
     }
0
 

Author Comment

by:jtgraphic
ID: 17163307
InteractiveMind's answer worked best.  I gave you others a few points for the effort.  I'm glad I learned how that worked :)
0
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 17163314
Thank you :)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question