• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

ArrayIndexOutOfBoundsException

 
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!
 
Ranch Hand
Posts: 86
2
VI Editor Java Ubuntu
  • 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).
 
lowercase baba
Posts: 12760
51
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!
 
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)

 
Any sufficiently advanced technology will be used as a cat toy. And this tiny ad contains a very small cat:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!