I have tested my code and I faced the below error
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
and the line 27 that caused the problem is
int number_of_runs = Integer.valueOf(args);
could someone please help me how to fix this error?
ArrayIndexOutOfBoundsException occurs when you try to access an element in an array which does not exists. In your case you are trying to access the first element but the array is empty that's the reason you are getting an ArrayIndexOutOfBoundsException.
That is hardly true. Assuming we're talking about the args passed from the command line, so, in case there are no arguments passed, args doesn't exist at all, hence it is not null and args.length supposed to be 0. But now question is, why error is:
arwa ali wrote:java.lang.ArrayIndexOutOfBoundsException: 1
if OP trying to access element at position 0, then I'd expect to see an:
Vaishakh Menon wrote:. . . trying to access the first element . . .
Second element. The first element is myArray. And as Liutauras (who beat me to it ) has told you, you won't get that exception from null elements in an array. In fact, I don't think it is possible to put nulls into the args array.
You can get nulls in an array of reference types quite easily; there are several ways to do it. Those in lines 1 and 2 are insidious; you might not realise that each of them contains three nulls:-