• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ArrayIndexOutOfBounds exception

 
Fantine Ponter
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Code Magnets in Head First Java, page 351:
The following code (Exercise solution straight from the book) compiles, but when I run it, I get the above exception on main because of the statement " String test = args[0];"

When I comment out that statement and uncomment my own statement just below that (String test = "no";) , I get no runtime error.

Please would someone explain what the statement, String test = args[0];
means, and why I get a runtime error. thanks in advance.

[edit]Disable smilies. CR[/edit]
[ December 05, 2008: Message edited by: Campbell Ritchie ]
 
Paul Clapham
Sheriff
Posts: 21152
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It gets the first of the parameters that you typed on the command line after the class name.

If you didn't type any parameters, then you get the error that you observe.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The String array in main contains the arguments you supply at runtime. If you do not supply any arguments, then this array will have a length of zero, and trying to access its elements will result in an exception.

To supply arguments at runtime, enter them on the command line after the class name. For example...

java ShowMyArgs here are some args
[ December 05, 2008: Message edited by: marc weber ]
 
Fantine Ponter
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Paul and Marc.

I'll try to be more observant in future!
 
fady zohdy
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
marc weber wrote:
To supply arguments at runtime, enter them on the command line after the class name. For example...


how can i supply arguments at run time when i am using an IDE like netbeans?!!!
the only way to run a java file in netbeans is through the run button which doesn't give you the chance to insert any arguments.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12149
31
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fady zohdy wrote:
marc weber wrote:
To supply arguments at runtime, enter them on the command line after the class name. For example...


how can i supply arguments at run time when i am using an IDE like netbeans?!!!
the only way to run a java file in netbeans is through the run button which doesn't give you the chance to insert any arguments.

a quick google search returned this article. It may be out of date, but on the other hand, it may be close enough to what you need.
 
Liutauras Vilda
Bartender
Pie
Posts: 2084
64
BSD VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fady zohdy wrote:how can i supply arguments at run time when i am using an IDE like netbeans?!!!


It is worth to remind you, that HFJ strongly advise you do not use such a clever IDE as NetBean or any other, until book is read and content understood. Instead is better to use simple text editor (vim, emacs, TextWrangler, Notepad++) and command line compiler.
It would help you better understand common use packages, classes, methods, import statements as well as compilation process, which can be a critical part of mastering language.
 
fady zohdy
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:

Instead is better to use simple text editor (vim, emacs, TextWrangler, Notepad++) and command line compiler.

first thanks for the reply.
i did use notepad at the beginning of the book but sometimes the code is so complicated and it needs a another java file to compile. i don't seem to be able to get the java compiler to read a whole package of java classes
 
fady zohdy
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:
a quick google search returned this article. It may be out of date, but on the other hand, it may be close enough to what you need.

done it using netbeans. the article was really helpful
thanks
 
J. Kevin Robbins
Bartender
Pie
Posts: 1800
28
Chrome Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fady zohdy wrote: i don't seem to be able to get the java compiler to read a whole package of java classes

And this is a classic example of why you should NOT be using an IDE. Until you learn to build, compile, and run you application outside of an IDE, you aren't really learning Java, you are learning to use a crutch. Please read the IDE FAQ, I think you'll find it quite useful.
 
fady zohdy
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
J. Kevin Robbins wrote: I think you'll find it quite useful.

i did use notepad and the java compiler at the beginning. i used to put all the .java files in a folder and the compiler would compile them in the same folder. i couldn't make a good structure for my directories and head first java didn't give hints about that. the structure netbeans use is very complicated and has files that i don't even know how to make like .xml file and .settings folder
 
Campbell Ritchie
Sheriff
Pie
Posts: 49472
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fady zohdy wrote: . . . i did use notepad . . .
You mean Microsoft Notepad? A dreadful program for coding. Have a look at this FAQ.
 
J. Kevin Robbins
Bartender
Pie
Posts: 1800
28
Chrome Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I recommend Notepad++ or one of the other editors listed in that FAQ. You'll get things like syntax highlighting and bracket matching, features that are helpful but still force you to understand what you are doing.

Run "javac -help" for a list of all the compiler options. You can use the "-d <directory>" option to specify where you want the compiled class files to go. I know it seems painful to do things "the hard way" instead of using an IDE, but in the end, you'll be glad you did. You'll come out of it with a better understanding of what the IDE is doing for you.
 
fady zohdy
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:You mean Microsoft Notepad? A dreadful program for coding.

my problem is not with the editor. my problem is with the directories structure and making the compiler put the compiled .class files in a specific folder automatically like what IDEs do.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49472
64
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try the -d option to the javac tool. Windows® page and *nix page for javac.

[edit]I see JKR has already suggested the -d option.[/edit]
 
fady zohdy
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Try the -d option to the javac tool.

got it ,thanks.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49472
64
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done sorting it out
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic