• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Head First Java Book

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
No, tomorrow we rule the world! With this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic