java missing return statement что за ошибка

Java Hungry

Java developers tutorials and coding.

[Solved] Missing return statement in java error

The missing return statement in java error is mostly faced by beginners. You can familiarize yourself with these kinds of common errors by coding simple java projects. Let’s understand the error first. This error occurs at compile-time. The root cause of the error as the name suggests when the return statement is missing in the program.

[Fixed] Missing return statement in java error

1. Method missing the return statement

I am using the HelloWorld java program as an example. If you compile the above code using below command

you will get the missing return statement error. There are two ways to fix it.

1.1 In printHelloWorld() method we have declared return type as String. But if you notice there is no return statement inside the printHelloWorld() method. Just add that as shown below.

Now compile and run your program using commands below

Yahoo!! your problem is resolved.

1.2 The above situation can also be solved by changing the return type to void without providing any return statement.

Now compile and run your program using below commands

Yahoo!! your problem is resolved.

2. Missing return statement error within if / while / for

If you declare return statement inside if / while / for but not at the end of the method containing them, then you will get missing return statement error as shown below.

If you compile the above code then you will get missing return statement error.

Fix it by adding the return statement for the method similar to the one we did in case 1 above i.e adding line return «Alive is Awesome»;

There is a special case, what happens when you provide return statements inside both if/else block, as shown below. What will be the output?

The above code will compile fine and you will get the following output:

That’s all for today. Please mention in comments in case you have any questions related to missing return statement in java error.

Источник

Compiling Error: Missing return statement [duplicate]

This program plays craps with 3 different methods. I need help in playing craps but i’m required to have these 3 different methods but for some reason every time I compile I am getting this error:

Читайте также:  что делать если еда застряла между зубов

Code:

8 Answers 8

Java must look at all execution paths. What happens if the while loop ends without returning anything? You may be logically preventing that, but the Java compiler won’t do that analysis.

You can add the last return statement after your code like this:

It will make it compile and your code will still work.

you only return statements are inside the body of an if block.

The compiler doesn’t know if any of those if blocks will ever be reached, so it’s giving you an error.

You might want to have a default return statement at the end

I’m assuming that if that default return statement ever actually gets executed, than it’s an error state, and you should react accordingly.

You are missing a return in your while block:

You should look into consistent code formatting. Not only for us, but for yourself as well so when you look at this code after a few weeks you can still read it.

When you have to add return make sure you add return for every possible execution path of add a default return statement. You program is missing both of them

Here is a very stripped down version of your code:

To fix the missing return statement, add a return statement to code3 or code4.

Источник

«Missing return statement» within if / for / while

I have a question regarding return statements used within if() while() or for() statements.

7 Answers 7

But if you write an if / else block and each one of them is having a return in it then the compiler knows that either the if or else will get executed and the method will return a value. So this time the compiler will not force you.

That’s because the function needs to return a value. Imagine what happens if you execute myMethod() and it doesn’t go into if(condition) what would your function return? The compiler needs to know what to return in every possible execution of your function.

Читайте также:  какой корректор добавить в краску для пепельного оттенка

Checking Java documentation:

Definition: If a method declaration has a return type then there must be a return statement at the end of the method. If the return statement is not there the missing return statement error is thrown.

This error is also thrown if the method does not have a return type and has not been declared using void (i.e., it was mistakenly omitted).

You can do this to solve your problem:

That is illegal syntax. It is not an optional thing for you to return a variable. You must return a variable of the type you specify in your method.

You are effectively saying: I promise any class can use this method (public) and I promise it will always return a String (String).

Then you are saying IF my condition is true I will return x. Well, that is too bad. There isn’t any IF in your promise. You promised that myMethod will always return a String.

Even if your condition is always true, the compiler has to assume that there is a possibility of it being false. Therefore you always need to put a return at the end of your non-void method outside of any conditions just in case all of your conditions fail.

Источник

Java: missing return statement after try-catch [duplicate]

The compiler is telling me:

But I thought the point of the catch statement was to be able to catch an error and return a string describing the error, without having to worry about matching the function’s return type.

4 Answers 4

Because you’re missing a return statement.

The method declares that it returns something, so it must return something (or throw an exception). The compiler can’t guarantee that any of the return statements in the try block will be reached if an exception is thrown before any of them execute. So the catch block also needs to return something (or throw an exception, or return something after the try/catch construct entirely).

Edit: Looking again, you’re also potentially missing a return in the try block. (If you don’t have one after the entire try/catch structure.) What if none of the conditions in the if/else structure are satisfied? Nothing is returned. Which is invalid.

Читайте также:  golang что это простыми словами

Basically, all logical paths must result in a valid exit of the method. You’ve missed two such paths.

You’re not returning anything in your function on several paths.

But I thought the point of the catch statement was to be able to catch an error and return a string describing the error, without having to worry about matching the function’s return type.

Also your code cna’t actually throw an IllegalArgumentException so your catch block will never get called. In this case, it sounds like what you want is instead something like this

Источник

Missing return statement when trying to return a boolean value

I keep getting an error in java and I don’t know why «missing return statement». What I’m trying to do is create a method with two parameters (String and char) check if the char appears in the string and return a boolean value based on the comparison. Here’s what I have so far:

Again, the error in the console is «missing return statement». Please be aware that I’m not an expert in Java and probably I have more errors in my code, my apologize in advance for that.

edit: Don’t look at the indentation, I just copy and paste the code here. edit 2: «h» is «Hello world» in my code.

3 Answers 3

You should assign the boolean result to a variable instead of returning it in the loop. Add the return statement after the for loop, this should fix your issue. HTH

That’s because of your if and else conditions are in a for loop. The for loop itself is a conditional loop.

There can be a scenario if your if or else stays unreached. You need to return a boolean outside of the for loop here.

Example scenario: ‘h’ is an empty String.

EDIT: And as per your statement «h» is «Hello world» in my code, at the time of compilation, the complier isn’t looking at the value that is being passed to ‘h’. Your compare method is an individual identity and it is not at all limited to a single use only.

Источник

Сказочный портал