• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ArrayIndexOutOfBoundError

 
abrar alvi
Ranch Hand
Posts: 66
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Marshal
Posts: 34071
331
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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.
 
abrar alvi
Ranch Hand
Posts: 66
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank Jeanne,


i will work out and see....
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 66
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 66
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 216
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abrar, its ArrayIndexOutOfBoundException Not ArrayIndexOutOfBoundError.
 
abrar alvi
Ranch Hand
Posts: 66
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nikhil Sagar wrote:Abrar, its ArrayIndexOutOfBoundException Not ArrayIndexOutOfBoundError.



yes it is...
 
Nikhil Sagar
Ranch Hand
Posts: 216
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 .
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic