The following code compiles, but I get an ArrayIndexOutofBoundsException error when I try to run it. I’m learning Java by reading the Thinking in Java-book, and I pretty much copied the code from the solutions guide. Help is very much appreciated.
public class MainLine
{
public static void main(String[] args){
System.out.println(args[0]);
System.out.println(args[1]);
System.out.println(args[2]);
}
First, ensure you pass the arg in command line.
To prevent exception, the better way is to check the length first.