How to fix “missing return statement” in Java

I'm making a Java method for an Android application that returns the directory where the application should work in an external directory

I encountered a problem when processing returns. I don't know how to solve the error of "missing return statement"

public String getpath() {
    String state = Environment.getExternalStorageState();
    if(Environment.MEDIA_MOUNTED.equals(state)) {
        String extdir = Environment.getExternalStorageDirectory().getAbsolutePath();
        File path = new File(extdir,"App");
        if(path.exists()) {
            return path.getAbsolutePath();
        }
        else {
            checkandmakepath();
            getpath();
        }
    }
    else {
        Toast.makeText(this,"Could not access External Directory!",Toast.LENGTH_LONG).show();
        Intent to_main = new Intent(this,MainActivity.class);
        startActivity(to_main);
        MainActivity.this.finish();
    }
}

Solution

public String getpath()
public String getpath()

This means that your method will return a string object, which is only completed in the if statement Put it at the end of the method to ensure that something is always returned no matter what conditions you experience (since you have returned in the if branch, you do not need to do this at the end of everything else.)

return null;

If you don't want to exit the method before returning the path, you can make a while loop for it, but this may cause it to be infinite, so it's best to call your method

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>