Fehlermeldungen des Compilers zu verstehen ist eine essentielle Fähigkeit und oft auch sehr einfach, da die Compiler sehr präzise geworden sind.
In diesem Kapitel werden ausgewählte Fehlermeldungen des Java Compilers (Temurin Java 25) vorgestellt.
Mit ein bisschen Übung werden Sie in der Lage sein die Fehlermeldungen schnell zu verstehen und die Fehler in Ihrem Code zu beheben. (Schneller als das Kopieren in den Chat Ihres AI-Assistenten.)
Falscher Typ
1int i = 0;23void main() {4byte b = i;5IO.println(b);6}
1Bugs.java:4: error: incompatible types: possible lossy conversion from int to byte2byte b = i;3^
Array Indizes
1int[] a = new int[10];23void main(String[] args) {4long b = Integer.parseInt(args[0]);5IO.println(a[b]);6}
1Bugs.java:5: error: incompatible types: possible lossy conversion from long to int2IO.println(a[b]);3^
Syntaxfehler
1void printElement(int[] a, long b) throws IOException {2IO.println(a[(int) b]);3threw new IOException("Simulierte Ausnahme");4}56void main(String[] args) {7long b = Integer.parseInt(args[0]);8int[] a = { 10, 20, 30 };9printElement(a, b);10}
1Bugs.java:3: error: not a statement2threw new IOException("Simulierte Ausnahme");3^4Bugs.java:3: error: ';' expected5threw new IOException("Simulierte Ausnahme");6^
Evolution von Java
1void printElement(int[] a, long b) throws IOException {2IO.println(a[(int) b]);3throw new IOException("Simulierte Ausnahme");4}56void main(String[] args) throws IOException {7int[] a = { 10, 20, 30 };8printElement(a, Integer.parseInt(args[0]););9}
1Bugs.java:1: error: class, interface, enum, or record expected2void printElement(int[] a, long b) throws IOException {3^4Bugs.java:3: error: class, interface, enum, or record expected5throw new IOException("Simulierte Ausnahme");6^7[... more errors ...]
static Kontext
1public class Bugs {23void printElement(int[] a, long b) {4System.out.println(a[(int) b]);5}67public static void main(String[] args) {8int[] a = { 10, 20, 30 };9printElement(a, Integer.parseInt(args[0]));10}11}
1Bugs.java:10: error: non-static method printElement(int[], long) cannot2be referenced from a static context3printElement(a, Integer.parseInt(args[0]));4^
Definition von Klassen
1public Class Util {2void printElement(int[] a, long b) {3System.out.println(a[(int) b]);4} }5public class Bugs {6public static void main(String[] args) {7printElement(new int[] { 10, 20, 30 }, Integer.parseInt(args[0]));8} }
1Bugs.java:1: error: class, interface, annotation type, enum,2record, method or field expected3public Class Util {4^5Bugs.java:3: error: class, interface, annotation type, enum,6record, method or field expected7} }8^
No Symbol
1public class Util {2static void printElement(int[] a, long b) {3System.out.println(a[(int) b]);4}5}6public class Bugs {7public static void main(String[] args) {8int[] a = { 10, 20, 30 };9printElement(a, Integer.parseInt(args[0]));10}11}
1Bugs.java:1012: error: cannot find symbol3printElement(a, Integer.parseInt(args[0]));4^5symbol: method printElement(int[],int)6location: class Bugs