File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes ArrayIndexOutOfBounds exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "ArrayIndexOutOfBounds exception" Watch "ArrayIndexOutOfBounds exception" New topic
Author

ArrayIndexOutOfBounds exception

Fantine Ponter
Ranch Hand

Joined: Aug 11, 2008
Posts: 30
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

Joined: Oct 14, 2005
Posts: 19693
    
  10

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

Joined: Aug 31, 2004
Posts: 11343

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 ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Fantine Ponter
Ranch Hand

Joined: Aug 11, 2008
Posts: 30
Thanks Paul and Marc.

I'll try to be more observant in future!
fady zohdy
Greenhorn

Joined: Jan 23, 2015
Posts: 29
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

Joined: Oct 02, 2003
Posts: 11815
    
  18

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Liutauras Vilda
Ranch Hand

Joined: Nov 12, 2014
Posts: 502
    
    5

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
Greenhorn

Joined: Jan 23, 2015
Posts: 29
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
Greenhorn

Joined: Jan 23, 2015
Posts: 29
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

Joined: Dec 16, 2010
Posts: 1404
    
  14

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.


"The good news about computers is that they do what you tell them to do. The bad news is that they do what you tell them to do." -- Ted Nelson
fady zohdy
Greenhorn

Joined: Jan 23, 2015
Posts: 29
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

Joined: Oct 13, 2005
Posts: 43377
    
  32
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

Joined: Dec 16, 2010
Posts: 1404
    
  14

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
Greenhorn

Joined: Jan 23, 2015
Posts: 29
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

Joined: Oct 13, 2005
Posts: 43377
    
  32
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
Greenhorn

Joined: Jan 23, 2015
Posts: 29
Campbell Ritchie wrote:Try the -d option to the javac tool.

got it ,thanks.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 43377
    
  32
Well done sorting it out
 
 
subject: ArrayIndexOutOfBounds exception
 
It's not a secret anymore!