This week's giveaway is in the Programmer Certification (OCPJP) forum.
We're giving away four copies of Java Mock Exams (software) and have David Mayer on-line!
See this thread for details.
Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1

 
arwa ali
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have tested my code and I faced the below error
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at psofc2.Main.main(Main.java:27)
and the line 27 that caused the problem is
int number_of_runs = Integer.valueOf(args[0]);
could someone please help me how to fix this error?
thanks
 
Vaishakh Menon
Greenhorn
Posts: 18
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Please make sure that you are executing the code:

javac filename.java
java filename 1

Please check this :
http://stackoverflow.com/questions/32042638/arrayindexoutofboundsexception-when-launching-a-java-program
 
Dana Ucaed
Ranch Hand
Posts: 148
3
Netbeans IDE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
args[0] is the first element of array args.

args is an array of String.

So args[0] is null.
 
Liutauras Vilda
Marshal
Posts: 3385
131
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dana Ucaed wrote:So args[0] is null.
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[0] 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:
java.lang.ArrayIndexOutOfBoundsException: 0
 
Campbell Ritchie
Marshal
Posts: 53094
124
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vaishakh Menon wrote:. . .  trying to access the first element . . .
Second element. The first element is myArray[0].  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:-
 
Dana Ucaed
Ranch Hand
Posts: 148
3
Netbeans IDE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interesting post about null.

Things don't seem what it appear.

I will try the example from Campbell.

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!