Free: 4 trials of 20 minutes, no cardStart free

Craftstack Academy
Java

Java error: missing return statement, and how to fix it

· 9 min read

Your method returns "pass" when the score is high enough. You compile, and javac says a return is missing:

Terminal
Main.java:6: error: missing return statement
    }
    ^
1 error

error: missing return statement means that there's at least one way through your method that reaches the end without returning a value. A method declared with a return type, like String or int, promises to always give a value back, on every path. Java checks this before running anything, and the ^ points at the closing brace where a path runs out. This article shows how to find that path and the six common shapes of this error.

Try it yourself

Save this as Main.java and run javac Main.java:

Main.java
public class Main {
    static String grade(int points) {
        if (points >= 50) {
            return "pass";
        }
    }

    public static void main(String[] args) {
        System.out.println(grade(72));
    }
}

You'll get the error above, even though grade(72) would return "pass". Java doesn't look at the value you pass. It looks at every possible path.

What the error means

The first line of a method sets a promise:

java
static String grade(int points)

The word before the name, String, is the return type. It says: whoever calls grade will always get a String back. If the method can reach its closing } without a return, the caller would get nothing, and Java won't allow that.

Read the message in parts:

  • Main.java:6: the line with the closing brace of the method.
  • missing return statement: some path through the method reaches this brace without returning.
  • The ^ under }: Java points at the end, because that's where the path runs out. The real fix is usually a few lines above.

So ask: if the if is false, or the loop finds nothing, what does this method return?

Cause 1: if without else

The example above. When points is 50 or more, the method returns. When it's less, the if is skipped, and the method reaches the end with nothing to return.

The fix: decide what the other case returns.

java
static String grade(int points) {
    if (points >= 50) {
        return "pass";
    }
    return "fail";
}

A return after the if covers every case the if didn't. You could also write else { return "fail"; }; both are correct.

Cause 2: else if that covers everything, but Java can't tell

This one feels unfair:

java
static String grade(int points) {
    if (points >= 50) {
        return "pass";
    } else if (points < 50) {
        return "fail";
    }
}
Terminal
Main.java:8: error: missing return statement
    }
    ^
1 error

You know that a number is either >= 50 or < 50. But Java doesn't analyse the conditions; it only sees two ifs, and after them a path where neither was true. The fix is to make the last case a plain else:

java
    if (points >= 50) {
        return "pass";
    } else {
        return "fail";
    }

This also makes the code clearer: the reader doesn't have to check that the two conditions cover everything.

Cause 3: a loop that returns only when it finds something

Searching is a classic case:

java
static int findIndex(int[] nums, int target) {
    for (int i = 0; i < nums.length; i++) {
        if (nums[i] == target) {
            return i;
        }
    }
}
Terminal
Main.java:8: error: missing return statement
    }
    ^
1 error

If the target is in the array, the method returns. If it isn't, the loop ends, and then what? Return a value that means "not found" after the loop:

java
    for (int i = 0; i < nums.length; i++) {
        if (nums[i] == target) return i;
    }
    return -1;

-1 is the standard "not found" value for indexes in Java; String.indexOf uses it too. The caller must check for it before using the result as an index.

Cause 4: printing instead of returning

A method can print the answer without returning it:

java
static int square(int x) {
    System.out.println(x * x);
}
Terminal
Main.java:4: error: missing return statement
    }
    ^
1 error

Printing shows a value on the screen; returning hands it back to the code that called the method. If the method should give back the result, write return x * x;. If it should only print, change the return type to void, which means "returns nothing".

The same happens when you calculate a result and forget the last step:

java
static int square(int x) {
    int result = x * x;
}

Add return result; before the closing brace.

Cause 5: try and catch

When the try part returns but the catch part doesn't, the method has a path without a return: the path where the exception happened.

java
static int parse(String s) {
    try {
        return Integer.parseInt(s);
    } catch (NumberFormatException e) {
        System.out.println("Not a number");
    }
}
Terminal
Main.java:8: error: missing return statement
    }
    ^
1 error

Decide what the method should give back when the text isn't a number. A default value is one option: return 0; inside the catch, or after it. In bigger programs, letting the exception go through to the caller is often better, but either way, every path must end in a return or an exception.

Cause 6: a switch without default

A classic switch with case labels needs a default when the method returns from inside it:

java
static String day(int n) {
    switch (n) {
        case 1: return "Mon";
        case 2: return "Tue";
    }
}
Terminal
Main.java:7: error: missing return statement
    }
    ^
1 error

For n = 3, no case matches, and the method reaches the end. Add default: return "?"; as the last case.

Newer Java versions have switch expressions with arrows, which must cover every case by design, so this error can't happen there:

java
static String size(int n) {
    return switch (n) {
        case 1 -> "S";
        case 2 -> "M";
        default -> "L";
    };
}

All of the fixed versions above compile and run. In our test with Java 21, grade(72), grade(30), findIndex with and without a match, day(2) and size(5) printed pass fail 1 -1 Tue L.

The opposite errors

Two related messages appear when the return doesn't fit the method:

java
static void greet(String name) {
    return "Hi " + name;
}
Terminal
Main.java:3: error: incompatible types: unexpected return value
        return "Hi " + name;
                     ^
1 error

A void method returns nothing, so returning a value is an error. Change void to String, or print the greeting instead of returning it.

And code after a return can never run:

Terminal
Main.java:4: error: unreachable statement
        System.out.println("done");
        ^
1 error

Move the line before the return, or delete it.

How the message looks in other tools

IntelliJ IDEA shows the same javac text with a java: prefix in its build output: java: missing return statement. Eclipse words it differently, as This method must return a result of type String (or int, and so on). Same cause, same fixes.

Errors that look alike

Wrong fixes that don't help

  • Adding return null; or return 0; at the end without thinking. It compiles, but now the caller may get a meaningless value. Choose a value that really means something, like -1 for "not found", and handle it.
  • Changing the return type to void when the caller needs the result. The error moves to the call.
  • Adding @SuppressWarnings. This is a compile error, not a warning: nothing can switch it off.

A debugging walkthrough

javac reports missing return statement at the closing brace of a 20-line method:

  1. Read the return type on the method's first line.
  2. List every return in the method.
  3. For each if, ask: what happens if it's false? For each loop: what if it never finds anything, or the array is empty? For each try: what if the catch runs?
  4. Find the path with no return. Usually one of the three above.
  5. Add a return with a meaningful value at the end, or an else, a default or a return in the catch.

Practice

Which of these compile?

  1. A method returning int whose only statement is while (true) { return 1; }.
  2. static boolean isAdult(int age) { if (age >= 18) return true; }
  3. static boolean isAdult(int age) { return age >= 18; }

Answers. (1) Compiles: Java knows while (true) only ends through the return. (2) missing return statement: no value when age is under 18. (3) Compiles, and it's the cleanest version: a comparison is already true or false.

Frequently asked questions

What does "missing return statement" mean in Java?

Your method has a return type, but at least one path through it reaches the end without a return. Every path must return a value.

Why does Java say missing return when I have a return?

Your return is inside an if, a loop or a try. Java sees a path where that block doesn't return, for example when the condition is false.

Can I return null to fix it?

For object types, yes, but only if the caller checks for null. Otherwise you trade this compile error for a NullPointerException later.

Is missing return statement a compile error?

Yes. javac stops, and the program doesn't run until every path returns.

Do void methods need a return?

No. A void method may end without return. You can still write return; (without a value) to leave early.

A quick checklist

  1. Read the return type on the method's first line.
  2. Find the path that reaches } without a return.
  3. if without else? Add a return after it.
  4. Loop that searches? Return a "not found" value after the loop.
  5. try/catch or switch? Make the catch and default return too.
  6. Only printing? Return the value, or make the method void.

For a method that works on any error, read how to read an error message. The official Java tutorial on returning a value explains return types.

1 beginner error a week

One common error, what it means and how to fix it, in a short email. Free, and you can unsubscribe in any email.

Learn by writing the code yourself.

Twenty minutes with the tutor, right in your browser. No card, nothing to install.

Start the free trial
A session: the tutor's chat on the left, the code editor with a Java program and its output on the right