Solved

Easy Question: Missing Return Statement

Posted on 2006-07-22
6
301 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
zeroMAx challenge 20 76
seriesUp challenge 7 107
tidtcpserver connection lost handle 2 47
C# Error - Add Failed 12 49
I know it’s not a new topic to discuss and it has lots of online contents already available over the net. But Then I thought it would be useful to this site’s visitors and can have online repository on vim most commonly used commands. This post h…
This article will show, step by step, how to integrate R code into a R Sweave document
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

758 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now