Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Head First Java Book  RSS feed

 
farukh ahmad
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am trying to run the final program in the HeadFirst Java Book by Kathy Sierra and Bert Bates and i get the following error.


Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at BeatBoxFinal.main(BeatBoxFinal.java:40)

The coding is as follows:

public static void main (String[] args) {
new BeatBoxFinal().startUp(args[0]); // args[0] is your user ID/screen name
}

can you please help

farukh
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch!

Within your main method, "args" references a String array holding any command line arguments that might be specified at runtime. These are entered after the class name when you run java from the command line. For example, if you type...

java MyProgram something else

...then within the main method of MyProgram, args[0] will reference the String "something" and args[1] will reference the String "else".

If you don't enter any command line arguments, then args will have a length of zero, and trying to reference the first element (args[0]) will result in the OutOfBoundsException you're seeing. So to solve this problem, enter something as a command line argument when you run the program.
[ January 26, 2007: Message edited by: marc weber ]
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

The program expects a command-line argument when you run it; i.e.,

java BeatBoxFinal userid
 
farukh ahmad
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Thanks for replying

I go to the command-line prompt and type java BeatBoxFinal farukh and i get

exception in thread main NoSuchClassDef error

if you can help as i am eager to learn as a hobby.

Farukh
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you type this, are you working in the same directory that you compiled in?

Could you copy the entire error message and post it here?
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!