Results 1 to 5 of 5
  1. #1
    Eleeist is offline Member
    Join Date
    May 2011
    Posts
    84
    Rep Power
    0

    Default Missing return statement... but there is one

    Hi,

    Trivial problem, I know. But somehow I cannot figure it out.

    Java Code:
    private static String[] checkAccountStatus() {
      
            try {
            
                String XMLUrl = "url";
    
                DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                DocumentBuilder builder  = factory.newDocumentBuilder();
                Document document = builder.parse(new URL(XMLUrl).openStream());
                XPath xpath = XPathFactory.newInstance().newXPath();
                XPathExpression expr = xpath.compile("//result/*/text()");
    
                Object result = expr.evaluate(document, XPathConstants.NODESET);
    
                NodeList nodes = (NodeList) result;
    
                String[] resultArray;
                resultArray = new String[nodes.getLength()];
    
                for (int i = 0; i < nodes.getLength(); i++) {
                    
                    resultArray[i] = nodes.item(i).getNodeValue();
    
                }
                
                return resultArray;
                
            } catch (Exception e) {
                e.printStackTrace();
            }
            
        }
    I get missing return statement error. But wait... don't I return resultArray?

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,240
    Rep Power
    19

    Default Re: Missing return statement... but there is one

    What happens if an Exception is thrown in the try block and execution passes to the catch(...) block? Where's the return statement then?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Eleeist is offline Member
    Join Date
    May 2011
    Posts
    84
    Rep Power
    0

    Default Re: Missing return statement... but there is one

    I can't put return resultArray in the catch block. So what I'm supposed to do?

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,240
    Rep Power
    19

    Default Re: Missing return statement... but there is one

    You can't and you shouldn't want to -- because if you hit the catch(...) block it means something has gone wrong and resultArray, if it's even initialized, doesn't contain valid data.

    You have two options here: throw the Exception instead of catching it, and let the caller deal with it. This approach seems more correct to me than the alternative, which is to return null or a contrived String[] from after the catch block. In the second case, the caller will have to test the validity of the return value.

    Have you gone through the Exceptions tutorial? Lesson: Exceptions (The Java™ Tutorials > Essential Classes)

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    Eleeist is offline Member
    Join Date
    May 2011
    Posts
    84
    Rep Power
    0

    Default Re: Missing return statement... but there is one

    I was throwing errors before, and then I decided to switch to try blocks... Now everything is clear.

    Thanks.

Similar Threads

  1. Missing return statement
    By Aenohe in forum New To Java
    Replies: 8
    Last Post: 03-07-2012, 01:03 PM
  2. Missing Return statement =[
    By avirunes in forum New To Java
    Replies: 6
    Last Post: 02-12-2011, 10:34 AM
  3. Missing return statement
    By gkoef in forum New To Java
    Replies: 8
    Last Post: 01-01-2011, 02:52 AM
  4. missing return statement
    By bayan in forum New To Java
    Replies: 6
    Last Post: 04-26-2010, 03:15 PM
  5. Missing Return Statement Error
    By darkblue24 in forum New To Java
    Replies: 13
    Last Post: 02-16-2010, 08:22 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •