Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Easy Question: Missing Return Statement

Posted on 2006-07-22
6
Medium Priority
?
327 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 100 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 100 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 1800 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying 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

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
Introduction to Processes
Screencast - Getting to Know the Pipeline

824 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