Solved

Easy Question: Missing Return Statement

Posted on 2006-07-22
6
307 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

785 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