Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes ArrayIndexOutOfBoundError Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "ArrayIndexOutOfBoundError" Watch "ArrayIndexOutOfBoundError" New topic
Author

ArrayIndexOutOfBoundError

abrar alvi
Ranch Hand

Joined: Feb 01, 2012
Posts: 66

hi, i am writing a simple sound program and i am facing the following problem:

Problem: i am not able to give two input, i.e after giving the first input and hitting enter i get an error and next input cannot be given.


Code:




Output with error:

enter first args
1
java.lang.ArrayIndexOutOfBoundsException: 0
at MiniMiniMusicCmdLine.main(MiniMiniMusicCmdLine.java:15)



please help me to understand in what i made mistake while doing the coding....



thanks
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30353
    
150

"args" already exists. It is the number of parameters you pass to your method from the command line. It isn't meant to be reused for another purpose or assigned to.

You can create a new array String[] mine = new String[2] to store data. Or just two String variables. There doesn't appear to be a reason to use the array.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
abrar alvi
Ranch Hand

Joined: Feb 01, 2012
Posts: 66

thank Jeanne,


i will work out and see....
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

The error message is telling you exactly what's wrong:


At line 15 of MiniMiniMusicCmdLine.java, you're accessing the 1st element (at index 0) of an array that has no elements.

There are a couple of problems here. First, you really shouldn't be writing to the args array. That's used for the JVM to pass command line arguments to your main() method. You should view it as read-only. That in itself won't cause the compiler error though. If the array was non-zero size (that is, if you had passed arguments when you invoked your program), you'd have been allowed to overwrite them. It's just not good practice to do so.

Arrays have a fixed size at creation. So if an array is created with new String[0] as the args array was here (by the JVM before it invoked your main() method), it will always be empty, and you can never access and element because there is no element to access.

If you want to write to an array, you should a) declare your own array variable, and b) initialize it to an appropriate size for what you're going to put into it.

But that brings us to what you're trying to do with that array in the first place:



There's really no need for an array there. Just a simple String variable will suffice for the result of the readLine() call.
abrar alvi
Ranch Hand

Joined: Feb 01, 2012
Posts: 66

Jeff Verdegan wrote:
That in itself won't cause the compiler error though. If the array was non-zero size (that is, if you had passed arguments when you invoked your program), you'd have been allowed to overwrite them. It's just not good practice to do so.

Arrays have a fixed size at creation. So if an array is created with new String[0] as the args array was here (by the JVM before it invoked your main() method), it will always be empty, and you can never access and element because there is no element to access.



Sorry but i did not understand your these quoted above lines...



Also these are some of the other doubts :

Does JVM create String[] args as empty.....


I did not get any compiler error while using args....
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

abrar alvi wrote:
Jeff Verdegan wrote:
That in itself won't cause the compiler error though. If the array was non-zero size (that is, if you had passed arguments when you invoked your program), you'd have been allowed to overwrite them. It's just not good practice to do so.

Arrays have a fixed size at creation. So if an array is created with new String[0] as the args array was here (by the JVM before it invoked your main() method), it will always be empty, and you can never access and element because there is no element to access.



Sorry but i did not understand your these quoted above lines...



Also these are some of the other doubts :

Does JVM create String[] args as empty.....


I did not get any compiler error while using args....


When you start the JVM, you can give it arguments to pass to your main() method via its args parameter. For instance, if I start MyClass's main() from the command line like this:


then in MyClass's main() method, args will have length 2, args[0] will be "abc" and args[1] will be "def". But if I just do:



then args will have length 0, and will not have any elements, which was the case when you ran your code.

If you're running your code from inside an IDE like Eclipse or NetBeans, there will be a place to set the command line args that you want to pass, just as if you had typed them on the command line. If you don't set anything there, it will be as if you didn't provide any arguments, and the args array will again have length 0.

Regardless of how you start it--command line or IDE--the JVM creates the args array appropriately to the arguments provided and invokes your main() method, passing a reference to that array.

You won't get any compiler error because you didn't do anything syntactically wrong. The compiler doesn't know what args's length will be. It can't know, because that's not decided until runtime.
abrar alvi
Ranch Hand

Joined: Feb 01, 2012
Posts: 66

Thanks Jeff for making me understand.....

yes i am using Net Beans.... but how do i set the command line args there....


also if i can pass directly the arguments while starting then i wont be requiring the InputStreamReader class ???
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

abrar alvi wrote:Thanks Jeff for making me understand.....

yes i am using Net Beans.... but how do i set the command line args there....


I don't know. I don't use NB. You should be able to find the setting if you poke through its menus and such, or check its documentation or help site.

also if i can pass directly the arguments while starting then i wont be requiring the InputStreamReader class ???


It's a choice of whether you want to provide those values from outside the program before it starts (command line args) or have the program actively read them once it's running. Command line args are usually used for initial configuration, while System.in is typically for providing input that you want the program to process. In your case, you can use either one. It really doesn't matter. Go with whatever is easiest or makes the most sense to you.
Nikhil Sagar
Ranch Hand

Joined: Apr 21, 2012
Posts: 216

Abrar, its ArrayIndexOutOfBoundException Not ArrayIndexOutOfBoundError.


OCPJP 6 86%
abrar alvi
Ranch Hand

Joined: Feb 01, 2012
Posts: 66

Nikhil Sagar wrote:Abrar, its ArrayIndexOutOfBoundException Not ArrayIndexOutOfBoundError.



yes it is...
Nikhil Sagar
Ranch Hand

Joined: Apr 21, 2012
Posts: 216

abrar alvi wrote:
Nikhil Sagar wrote:Abrar, its ArrayIndexOutOfBoundException Not ArrayIndexOutOfBoundError.



yes it is...


Abrar, I am not asking, i am telling you and correcting you that it is ArrayIndexOutOfBoundException Not ArrayIndexOutOfBoundError .
 
wood burning stoves
 
subject: ArrayIndexOutOfBoundError