Your method returns "pass" when the score is high enough. You compile, and javac says a return is missing:
Main.java:6: error: missing return statement
}
^
1 errorerror: 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:
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:
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.
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:
static String grade(int points) {
if (points >= 50) {
return "pass";
} else if (points < 50) {
return "fail";
}
}Main.java:8: error: missing return statement
}
^
1 errorYou 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:
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:
static int findIndex(int[] nums, int target) {
for (int i = 0; i < nums.length; i++) {
if (nums[i] == target) {
return i;
}
}
}Main.java:8: error: missing return statement
}
^
1 errorIf 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:
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:
static int square(int x) {
System.out.println(x * x);
}Main.java:4: error: missing return statement
}
^
1 errorPrinting 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:
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.
static int parse(String s) {
try {
return Integer.parseInt(s);
} catch (NumberFormatException e) {
System.out.println("Not a number");
}
}Main.java:8: error: missing return statement
}
^
1 errorDecide 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:
static String day(int n) {
switch (n) {
case 1: return "Mon";
case 2: return "Tue";
}
}Main.java:7: error: missing return statement
}
^
1 errorFor 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:
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:
static void greet(String name) {
return "Hi " + name;
}Main.java:3: error: incompatible types: unexpected return value
return "Hi " + name;
^
1 errorA 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:
Main.java:4: error: unreachable statement
System.out.println("done");
^
1 errorMove 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
variable might not have been initialized: the same "some path" logic, applied to variables instead of return values. See variable might not have been initialized.cannot find symbol: often when the method name is misspelled at the call. See cannot find symbol.incompatible types: String cannot be converted to int: the method returns, but the wrong type. See String cannot be converted to int.
Wrong fixes that don't help
- Adding
return null;orreturn 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-1for "not found", and handle it. - Changing the return type to
voidwhen 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:
- Read the return type on the method's first line.
- List every
returnin the method. - 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 eachtry: what if thecatchruns? - Find the path with no
return. Usually one of the three above. - Add a
returnwith a meaningful value at the end, or anelse, adefaultor areturnin thecatch.
Practice
Which of these compile?
- A method returning
intwhose only statement iswhile (true) { return 1; }. static boolean isAdult(int age) { if (age >= 18) return true; }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
- Read the return type on the method's first line.
- Find the path that reaches
}without areturn. ifwithoutelse? Add areturnafter it.- Loop that searches? Return a "not found" value after the loop.
try/catchorswitch? Make thecatchanddefaultreturn too.- 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.
