'Error: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0
I'm new to Java and I'm facing this error when trying to start the application.
My code looks like this:
public class Testen {
public static void main( String[] args){
int a = Integer.parseInt(args[0]);
System.out.println(a);
}
}
I've already read threads of people who were facing similar errors, however, I'm failing to apply their solutions to my code.
Solution 1:[1]
As someone is the comments pointed out, when you run java code from command line like this: java testen arg0 arg1 arg2 arg3, then these arguments are stored inside the the string array String[] args. To access the arg0 you can use args[0] in your code.
If you run java testen then your string array args will be empty, so you can't access an element that doesn't exist, and you get the exception.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Jhanzaib Humayun |
