Java - Missing return statement

后端 未结 3 601
南方客
南方客 2021-01-29 10:01

I get an error - \"missing return statement\" - on line 26, which in this case is the last curly bracket in the code. I know that it literally means that I have to return somewh

3条回答
  •  再見小時候
    2021-01-29 10:07

    Ok first of all please note that this is not really a beginners forum for learning any programming language. You will find it very hard to get a very basic question answered here without approximately 20 people pointing you in the direction of Google (myself included)

    That being said please look for java tutorials, beginners guides, or simply the docs. The answer will almost everytime jump you straight in the face since there is just so much material on the java programming language

    Stackoverflow is much more a question and answer site for more abstract problems, never-seen-before issues and tricky nuts for experts - all things that are likely to cater to a general audience and to help many people, not just yourself. Admittedly you posed your question quite nicely and formatted it well, so kudos to you and welcome to the site

    As for this very specific problem you have the following options:

    Declare a void return type

    public void openFile() throws Exception {
    
    }
    

    Return a preliminary null until you're sure what to return

    public String[] openFile() throws Exception {
        return null;
    }
    

    (But make sure that the calling entity knows what's coming back ;))

    Return some valid data to the entity that called the method

    public String[] openFile() throws Exception {
        String[] myArray = new String[5];
        return myArray;
    }
    

提交回复
热议问题