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

от admin

Understanding Common Errors In Java

When we were building our automated common error feedback feature on Mimir Classroom, we analyzed millions of stack traces that our students received when completing their coursework on our platform. In this post, you will find the errors we found to be most troubling to students in Java and some tips on how to approach them.

To start off, here is a quick refresher on how to read an error in the Java stack trace.

JavaStackTrace

1. ’;’ expected

This error means that you forgot a semicolon in your code. If you look at the full error statement, it will tell you where in your code you should check within a few lines. Remember that all statements in Java must end in a semicolon and elements of Java like loops and conditionals are not considered statements.

2. cannot find symbol

This error means that Java is unable to find an identifier that it is trying to work with. It is most often caused by the following:

  • You misspelled a variable name, class name, or a keyword somewhere. Remember that Java is case sensitive.
  • You are trying to access a variable or class which does not exist or can not be accessed.
  • You forgot to import the right class.

3. illegal start of expression

This error usually occurs when your code does not follow Java’s standard structure. Check the nesting of your variables and ensure that all your < >and ( ) are in the right place..

4. class, interface, or enum expected

This error is usually caused by misplaced < >. All code in Java needs to be contained within a class, interface, or enum. Ensure that all of your code is within the < >of one of those. We often have seen this error when there is an extra > at the end of your code.

5. reached end of file while parsing

This error usually occurs when you are missing a > somewhere in your code. Ensure that for every < you have one >in the right place.

6. missing return statement

This error usually means one of two things:

  • Your method is expecting a return statement but is missing one. Ensure that if you declared a method that returns something other than void and that it returns the proper variable type.
  • Your return statements are inside conditionals who’s parameters may not be reached. In this case, you will need to add an additional return statement or modify your conditionals.

7. ‘else’ without ‘if’

This error means that Java is unable to find an if statement associated to your else statement. Else statements do not work unless they are associated with an if statement. Ensure that you have an if statement and that your else statement isn’t nested within your if statement.

8. ‘(‘ expected or ‘)’ expected

This error means that you forgot a left or right parenthesis in your code. If you look at the full error statement, it will tell you where in your code you should check. Remember that for every ( you need one ).

9. case, default, or ‘>’ expected

This error means that your switch statement isn’t properly structured. Ensure that your switch statement contains a variable like this: switch (variable). Also ensure that every case is followed by a colon (:) before defining your case.

10. ‘.class’ expected

This error usually means that you are trying to declare or specify a variable type inside of return statement or inside of a method calls. For example: “return int 7;» should be «return 7;»

11. invalid method declaration; return type required

Every Java method requires that you declare what you return even if it is nothing. «public getNothing()» and «public static getNumber()» are both incorrect ways to declare a method.

The correct way to declare these is the following: «public void getNothing()» and «public static int getNumber()»

12. unclosed character literal

This error occurs when you start a character with a single quote mark but don’t close with a single quote mark.

13. unclosed string literal

This error occurs when you start a string with a quotation mark but don’t close with a second quotation mark. This error can also occur when quotation marks inside strings are not escaped with a backslash.

14. incompatible types

This error occurs when you use the wrong variable type in an expression. A very common example of this is sending a method an integer when it is expecting a string. To solve this issue, check the types of your variables and how they are interacting with other types. There are also ways to convert variable types.

15. missing method body

This error commonly occurs when you have a semicolon on your method declaration line. For example «public static void main(String args[]);». You should not have a semicolon there. Ensure that your methods have a body.

16. unreachable statement

This error means that you have code that will never be executed. Usually, this is after a break or a return statement.

Java Common Errors with Explanations

Here is the list of the most common Java errors we have seen students experience in Mimir Classroom. The list of errors will continue to grow as we learn from students’ work and as we hear from instructors like you send recommendations.

This typically indicates that one of your functions was declared to have a return type (non-void function) but it is not returning a value. Check that every non-void function returns the expected value.

This error can happen due to a multitude of issues, start by checking that all the required libraries are imported, that all the files names are correct, and that your code compiles (in the case of a project).

This means that one of your double quotation marks are invalid Unicode characters. C and C++ don’t allow «smart» quotes (i.e. ” vs «). This usually happens when you copy and paste code from other sources or use a text editor that uses Unicode. To fix these error just replace the Unicode quotation marks using your keyboard.

This is typically a syntax error. Make sure that for every opening parentheses or bracket there is a matching closing one and that every statement ends with a semi-colon.
You can read more about semi-colons and statements here: http://www.cplusplus.com/doc/tutorial/control/

This error refers to your use of the subscript operator to access values in an array, pointer of vector. For example, you’ve declared a single-dimensional array but you’re trying access it as a two-dimensional:

This means the file that the compiler is trying to compile doesn’t exist. Make sure the file you’re calling exists in the directory you’re in and that the extensions are either «.c» or «.h».

This is typically a syntax error. Make sure that for every opening parentheses or bracket there is a matching closing one and that every statement ends with a semi-colon.

You can read more about semi-colons and statements here: http://www.cplusplus.com/doc/tutorial/control/

This error is an indication that your if-else conditional statements are missing parentheses. You can read more about if-else statement syntax here: http://www.cplusplus.com/doc/tutorial/control/

Some older versions of C don’t allow ‘bool’ as variable types. If you believe your version of C allows it, make sure you’re importing the correct libraries. You can read more about using boolean variables in C here: https://en.cppreference.com/w/c/types/boolean

This usually happens when you copy and paste code from other sources or use a text editor that uses Unicode. To fix these error just replace the Unicode quotation marks using your keyboard.

This typically means that the placement of your parentheses and curly brackets is incorrect. Make sure you’re using the correct syntax, that your statements end with a semi-colon, and that for every opening parentheses and curly bracket has a closing one.

Typically this refers to the amount and type of arguments passed into a function do not match the expected amount and types in the function declaration.

You can read more about function arguments and syntax here: http://www.cplusplus.com/doc/tutorial/functions/

This usually happens when you copy and paste code from other sources or use a text editor that uses Unicode. To fix these error just replace the Unicode quotation marks or white spaces using your keyboard.

This usually happens when you copy and paste code from other sources or use a text editor that uses Unicode. To fix these error just replace the Unicode quotation marks or white spaces using your keyboard.

This error means that you forgot a semicolon in your code. If you look at the full error statement, it will tell you where in your code you should check within a few lines. Remember that all statements in Java must end in a semicolon and elements of Java like loops and conditionals are not considered statements.

This error means that Java is unable to find an identifier that it is trying to work with. It is most often caused by the following:

You misspelled a variable name, class name, or a keyword somewhere. Remember that Java is case sensitive.

You are trying to access a variable or class which does not exist or can not be accessed.

You forgot to import the right class.

This error usually occurs when your code does not follow Java’s standard structure. Check the nesting of your variables and ensure that all your < >and ( ) are in the right place.

This error is usually caused by misplaced < >. All code in Java needs to be contained within a class, interface, or enum. Ensure that all of your code is within the < >of one of those. We often have seen this error when there is an extra > at the end of your code.

This error usually occurs when code is placed outside of a method. Check your

This error usually occurs when you are missing a > somewhere in your code. Ensure that for every < you have one >in the right place.

Читать:
Как определить что за домом следят опера

This error usually means one of two things:

Your method is expecting a return statement but is missing one. Ensure that if you declared a method that returns something other than void and that it returns the proper variable type. (image 1)

Your return statements are inside conditionals who’s parameters may not be reached. In this case, you will need to add an additional return statement or modify your conditionals. (image 2)

This error means that Java is unable to find an if statement associated to your else statement. Else statements do not work unless they are associated with an if statement. Ensure that you have an if statement and that your else statement isn’t nested within your if statement.

Here is some more information on structuring control flow statements: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/if.html

This error means that you forgot a left parenthesis ‘(‘ in your code. If you look at the full error statement, it will tell you where in your code you should check. Remember that for every opening parenthesis ‘(‘ you need a closing one ‘)’.

This error means that you forgot a right parenthesis ‘)’ in your code. If you look at the full error statement, it will tell you where in your code you should check. Remember that for every opening parenthesis ‘(‘ you need a closing one ‘)’.

This error means that your switch statement isn’t properly structured. Ensure that your switch statement contains a variable like this: switch (variable). Also ensure that every case is followed by a colon (:) before defining your case.

This error usually means that you are trying to declare or specify a variable type inside of return statement or inside of a method calls. For example:

«return int 7;» should be «return 7;»

Every Java method requires that you declare what you return even if it is nothing.

«public getNothing()» and «public static getNumber()» are both incorrect ways to declare a method.

The correct way to declare these is the following: «public void getNothing()» and «public static int getNumber()»

This error occurs when you start a character with a single quote mark but don’t close with a single quote mark.

Here is some more information on characters on other primitive data types: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

This error occurs when you use the wrong variable type in an expression. A very common example of this is sending a method an integer when it is expecting a string. To solve this issue, check the types of your variables and how they are interacting with other types. There are also ways to convert variable types.

This error occurs when you start a string with a quotation mark but don’t close with a second quotation mark. This error can also occur when quotation marks inside strings are not escaped with a backslash.

This error commonly occurs when you have a semicolon on your method declaration line. For example «public static void main(String args[]);». You should not have a semicolon there. Ensure that your methods have a body.

This error means that you have code that will never be executed. Usually, this is after a break or a return statement.

This error means that you have declared a do-while loop and Java cannot detect the while part of the loop declaration. Ensure that you have a while statement and it is properly nested in your code.

This means that one of your double quotation marks are invalid Unicode characters. This usually happens when you copy and paste code from other sources or use a text editor that uses Unicode. To fix these error just replace the Unicode quotation marks using your keyboard.

This means that one of your double quotation marks are invalid Unicode characters. This usually happens when you copy and paste code from other sources or use a text editor that uses Unicode. To fix these error just replace the Unicode quotation marks using your keyboard.

Fix Missing Return Statement Error Type in Java

Fix Missing Return Statement Error Type in Java

The Java compiler might report a missing return statement error when using the if , for , or while loops in a method. This article explains why this happens and how to deal with it.

Why Does the missing return statement Error Occur in Java

One of the most common Java errors is a missing return statement .

It is an error that occurs during the compilation process. As the statement implies, the issue is usually caused by a missing return statement in the program.

Let’s take a program for reference that can produce this error.

Here is a method that returns a String type variable after determining whether or not a number passed as an argument is prime. This method utilizes the if , else , and for loops.

When we look at this program carefully, we can see the first pair of the if-else loop, each returning a String . In the else loop, we run a for loop that in turn returns a String using the if and else .

It looks alright at first glance. But after careful examination, we can find that the else statement doesn’t return anything in case the for loop fails.

In other words, the compiler doesn’t know that the for loop inside the else loop will return a String . It creates ambiguity.

Even if the for loop runs and returns a type, the compiler has to assume that there’s a possibility of it not returning anything. So to clear this confusion for the compiler, the else method should return a String type.

Java documentation suggests that if we declare a method with a return type, there needs to be a return statement at the end of the method. Otherwise, the compiler will show a missing return statement error.

This error is thrown when we mistakenly omit the return statement of the method, as it does not have a return type or has not been declared using the void type.

Missing return statement in java

In Java, missing return statement is a common error which occurs if either we forget to add return statement or use in the wrong scenario.

In this article, we will see the different scenarios when missing return statement can occur.

Table of Contents

Missing return statement

Here are the few scenarios where we can get Missing return statement error.

Scenario 1

Let’s take an example, where we have a method that returns absolute value of the given positive number. In this example, we computed the absolute value but forgot to return it. When we compile this code, the compiler reports an error. See the example and output.

Solution

We can solve it by using two ways, either add return statement in the code or set return type as void in the method signature. See the examples below, wherein the first example we have added the return statement. We have also added another method getAbsolute2() and returned void from it in case we don’t want to return anything from the method.

Scenario 2

This error can also occur if we have used a return statement in if block. This return statement will execute only when if block executes. Since return statement depends on the if block condition, compiler reports an error. We have to either put return statement in else block or to the end of the method to resolve this.

Note: A method that has return type in its signature must have return statement.

Solution

If return statement is inside any block like if , for etc. then we need to add an extra return either in else block or in the last line of method body to avoid missing return type error. See the example and output.

That’s all about Missing return statement in java.

Was this post helpful?

Share this

Cannot find symbol Java

[Fixed] bad operand types for binary operator in java

Related Posts

Author

Related Posts

[Fixed] Unsupported class file major version 61 in Java

Table of ContentsReason for Unsupported class file major version 61 in JavaSolution for Unsupported class file major version 61 in JavaAndorid studio/Intellij Idea with gradleAny other scenario In this post, we will see how to fix Unsupported class file major version 61 in Java. Reason for Unsupported class file major version 61 in Java You […]

java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList

[Fixed] java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList

Table of ContentsReason for java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayListFixes for java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayListUse ArrayList’s constructorAssign Arrays.asList() to List reference rather than ArrayList In this post, we will see how to fix java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList. ClassCastException is runtime exception which indicate that code has tried to […]

HashMap values cannot be cast to list

[Fixed] java.util.HashMap$Values cannot be cast to class java.util.List

Table of ContentsWhy HashMap values cannot be cast to list?Fix for java.util.HashMap$Values cannot be cast to class java.util.List In this post, we will see how to fix error java.util.HashMap$Values cannot be cast to class java.util.List. Why HashMap values cannot be cast to list? HashMap values returns java.util.Collection and you can not cast Collection to List […]

Unable to obtain LocalDateTime from TemporalAccessor

[Fixed] Unable to obtain LocalDateTime from TemporalAccessor

Table of ContentsUnable to obtain LocalDateTime from TemporalAccessor : ReasonUnable to obtain LocalDateTime from TemporalAccessor : FixLocalDate’s parse() method with atStartOfDay()Use LocalDate instead of LocalDateTime In this article, we will see how to fix Unable to obtain LocalDateTime from TemporalAccessor in Java 8. Unable to obtain LocalDateTime from TemporalAccessor : Reason You will generally get […]

[Solved] Variable might not have been initialized in Java

Learn about how to solve Variable might not have been initialized in Java.

[Solved] Class names are only accepted if annotation processing is explicitly requested

Learn about how to fix class names are only accepted if annotation processing is explicitly requested in java.

Похожие статьи