• Post Reply Bookmark Topic Watch Topic
  • New Topic

ArrayIndexOutOfBoundsException  RSS feed

 
Danny Roberts
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

This is my code

The exception i'm getting is:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at ExTestDrive.main(ExTestDrive.java:8)

I don't understand why i am getting this exception, any explanation would be much appreciated!
 
Tomas Linhart
Ranch Hand
Posts: 86
2
Java Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do you execute the application? You are trying to access first command line argument on line

but you didn't supply it, so that's why you are getting the exception.
 
Danny Roberts
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well from what i understand writing String test = args[0] should allow me to assign the first input in the CMD to the args[0], for example. However, i can't even run the program due to the exception so i fail to see how i am supposed to input anything. (These are all code blocks from the "Head First" java book so they should work).
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Danny Roberts wrote: However, i can't even run the program due to the exception so i fail to see how i am supposed to input anything.

That doesn't make sense. You cannot get an exception UNTIL you run the program.

My GUESS (and it is only a guess since you are not telling us) is that you are running it like this:

java ExTestDrive


and getting something like this:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at ExTestDrive.main(ExTestDrive.java:8)


Try running it like this:

java ExTestDrive yes


 
Danny Roberts
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brilliant, thank you!
 
chandra varma
Greenhorn
Posts: 2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you are run the application without IDE

java "your class namw" true(where true is the argument)

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!