Java error messages

Error messages in the console when compiling and running code are not always self-explanatory. To give you a better overview of what the errors mean and how to fix them, the most common errors are listed here.

Depending on the Java version you are using, messages may appear in English or German.

What should I do if I still cannot figure it out?

Look up the error message:
You can also use the browser built into 5Code for this.

Ask your tutors:
If no solution can be found and fellow students cannot help either, the tutors are always happy to assist.

Compile errors

Array required, but <X> found

array required but String found
    String b = a[0];
            ^

Possible causes

Possible solutions

ERROR_array_required

Cannot find symbol

cannot find symbol
    positionX = x;
    ^
symbol: variable positionX
location: class Block

Possible causes

Possible solutions

Note:

The search feature can help you find a variable and its usage more quickly.

ERROR_cannot_find_symbol

class, interface, enum, or record expected

class, interface, enum, or record expected
  }
  ^

Possible causes

Possible solutions

Note:

The extra braces are usually found at the end of the code for this error. Proper code indentation can help you find and avoid this error.

ERROR_class_expected

class, interface, annotation type, enum, record, method or field expected

class, interface, annotation type, enum, record, method or field expected
publi void begruessung(String name) {
^

Possible causes

Possible solutions

ERROR_class_interface_expected

Invalid method declaration; return type required

invalid method declaration; return type required
begruessung(String name) {
^

Possible causes

Possible solutions

ERROR_invalid_method_declaration

abc

Class <X> is public, should be declared in a file named <X>.java

class Block is public, should be declared in a file named Block.java
public class Block {
       
^

Possible causes

Possible solutions

Note:

When renaming a class, the corresponding file must also be renamed. 5Code automatically renames the class when you rename the file.

ERROR_class_file_name

Duplicate case label

duplicate case label
    case 's': ausgabe = "Daten werden gespeichert";
    ^

Possible causes

Possible solutions

Note:

When copying and pasting code from another location, make sure to adjust the code at all relevant places. (Ideally, avoid copy & paste!)

ERROR_duplicate_case_label

"else" without "if"

'else' without 'if'
    else {
    ^

Possible causes

Possible solutions

ERROR_else_without_if

Illegal start of expression/type

illegal start of expression
    void dreheAufWinkel(int winkel) {
    ^
illegal start of type
    return str;
    ^

Possible causes

Possible solutions

Note:

Proper code indentation can help you find and avoid this error. The cause of the error is very likely found in the lines before the error.

ERROR_illegal_start

Incompatible types

incompatible types: String cannot be converted to int
    int a = "a";
            ^
incompatible types: possible lossy conversion from double to int
    int x = Math.pow(2,4);
                    ^

Possible causes

Possible solutions

Note:

Floating-point numbers can also be converted to integers, e.g. int pi = (int)3.14159. Note that the values will be rounded. Strings consisting of numbers can also be converted, e.g. Integer.parseInt("500").

ERROR_incompatible_types

Invalid method declaration; return type required

invalid method declaration; return type required
    public toString() {
           ^

Possible causes

Possible solutions

Note:

Even if the method does not return a value, it must be explicitly declared as "void".

ERROR_return_type

Method <X> is already defined in class <Y>

method toString() is already defined in class Block
    public String toString() {
                  ^

Possible causes

Possible solutions

ERROR_method_already_defined

Method <X> in class <Y> cannot be applied to given types

method positioniereAuf in class Block cannot be applied to given types;
    block.positioniereAuf(2);
         ^
required: int,int
found:    int
reason: actual and formal argument lists differ in length

Possible causes

Possible solutions

Note:

The error message details which parameters are expected and which were provided. The search feature can help you find the method definition more quickly.

ERROR_cannot_be_applied_to_types

Missing return statement

missing return statement
    }
    ^

Possible causes

Possible solutions

Note:

If different values are returned under certain conditions (if), the other conditions must be introduced with "else if". Otherwise the compiler cannot guarantee that a value is returned in every case.

ERROR_missing_return

Non-static variable <X> cannot be referenced from a static context

non-static variable name cannot be referenced from a static context
    name = "Alice";
    ^

Possible causes

Possible solutions

ERROR_non_static_variable

Not a statement

not a statement
    abc
    ^

Possible causes

Possible solutions

ERROR_not_a_statement

Package <X> does not exist

package aufgaben does not exist
    import aufgaben.*;
    ^
package system does not exist
    system.out.println("Hello world");
          ^

Possible causes

Possible solutions

ERROR_package_does_not_exist

Reached end of file while parsing

reached end of file while parsing
    }
     ^

Possible causes

Possible solutions

Note:

Proper code indentation can help you find and avoid this error.

ERROR_end_of_file

Unclosed character literal

unclosed character literal char
    zeichen = 'a;
              ^

Possible causes

Possible solutions

ERROR_unclosed_character_literal

Unreachable statement

unreachable statement
    System.out.println(str);
    ^

Possible causes

Possible solutions

ERROR_unreachable_statement

uses unchecked or unsafe operations

Note: [...]\Super.java uses unchecked or unsafe operations.

What causes this warning?

How can I fix this warning?

ERROR_unsafe_operations

Variable <X> might not have been initialized

variable str might not have been initialized
    System.out.println(str);
                       ^

Possible causes

Possible solutions

Note:

If variables are initialized under certain conditions (if), the other conditions must be introduced with "else if". Otherwise the compiler cannot guarantee that the variable is initialized in every case.

ERROR_var_not_initialized

Variable <X> is already defined in method <Y>

variable a is already defined in method main(String[])
    int a = 9;
       ^

Possible causes

Possible solutions

Note:

Within a block, duplicate variable names are not allowed, even if they have different types.

ERROR_var_already_defined

<identifier> expected

<identifier> expected
    System.out.println("Hello world");
                      ^

Possible causes

Possible solutions

Note:

Proper code indentation can help you find and avoid this error.

ERROR_identifier_expected

<X> expected

';' expected
    position
X = 0
                  ^

Possible causes

Possible solutions

ERROR_expected

<X> has private access in <Y>

name has private access in Person
    String name = person.name;
                        ^

Possible causes

Possible solutions

ERROR_private_access

<X> in <Y> has been deprecated

[deprecation] stop() in Thread has been deprecated
    t.stop();
     ^
[removal] Integer(int) in Integer has been deprecated and marked for removal
    Integer i = new Integer(5);
               ^

What causes this warning?

How can I fix this warning?

ERROR_deprecation

Bad operand type <X> for unary operator "<Y>"

bad operand type boolean for unary operator '++'
        bool++;
            ^

Possible causes

Possible solutions

ERROR_bad_operand

Incomparable types: <X> and <Y>

incomparable types: boolean and int
        boolean bool3 = bool == 8;
                             ^

Possible causes

Possible solutions

ERROR_incomparable_types

Unnamed classes are a preview feature and are disabled by default.

unnamed classes are a preview feature and are disabled by default.
    void hello() {
    ^

Possible causes

Possible solutions

ERROR_unnamed_classes

Duplicate class: X

duplicate class: Test
class Test {
^

Possible causes

Possible solutions

ERROR_duplicate_class

Array dimension missing

array dimension missing
        int[] numbers = new int[];
                                 ^

Possible causes

Possible solutions

ERROR_array_dimension_missing

'.class' expected

'.class' expected
        this.zahl = int _zahl;
                             ^

Possible causes

Possible solutions

ERROR_dot_class_expected

Compact source file does not have main method in the form of void main() or void main(String[] args)

void mains() {
^

Possible causes

Possible solutions

ERROR_compact_source_file_main

Runtime errors

ArrayIndexOutOfBoundsException: <X>

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 4 out of bounds for length 2

Possible causes

Possible solutions

Note:

Array indices go from 0 to array length - 1.

ERROR_array_oob

ClassCastException: <X>

Exception in thread "main" java.lang.ClassCastException: class java.lang.Integer cannot be cast to class java.lang.String (java.lang.Integer and java.lang.String are in module java.base of loader 'bootstrap')

Possible causes

Possible solutions

ERROR_class_cast

NullPointerException

Exception in thread "main" java.lang.NullPointerException

Possible causes

Possible solutions

ERROR_null_pointer

StringIndexOutOfBoundsException: <X>

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: begin 0, end 4, length 1

Possible causes

Possible solutions

Note:

String indices go from 0 to string length - 1.

ERROR_string_oob

by zero

Exception in thread "main" java.lang.ExceptionInInitializerError Caused by: java.lang.ArithmeticException: / by zero

Possible causes

Possible solutions

ERROR_divide_by_zero