Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Easy Question: Missing Return Statement

Posted on 2006-07-22
6
Medium Priority
?
325 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
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…
Progress

722 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