• Post Reply Bookmark Topic Watch Topic
  • New Topic

How can 0 be out of bounds? (Exception error strikes again)  RSS feed

 
Laura Tobin
Greenhorn
Posts: 18
Eclipse IDE Java Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0

is what I get when I try to run my code.

I have retyped it I don't know how many times. I have run it with a try-catch, and without.
Here's the code that's giving me fits. I don't think moving a bracket is gonna fix it this time. Also how can 0 be out of bounds when it's pretty much the only thing that's ALWAYS in the Array?

I'm supposed to be able to load up the file and type in an argument. Then it returns the output based on the argument.
I can't get it to go past that part, so I can't put in my arguments.




 
Stevens Miller
Bartender
Posts: 1445
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Laura Tobin wrote:Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0


Like this:



Output:



"0" is a valid index for an array with at least one element in it. If the array as no elements, then "0" (which would index its first element, an element that doesn't exist in an empty array) would be out of bounds.
 
Laura Tobin
Greenhorn
Posts: 18
Eclipse IDE Java Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Heh. Sorry, I accidentally saved the post before adding my code.

I see what you are saying, but the circumstance I have is that there's user-input arguments...I don't know. I have only just started working with arrays and I feel like I'm missing something big.
 
Stevens Miller
Bartender
Posts: 1445
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, I posted my reply when your original post only had one line and no code in it.

What are you getting at run-time? And what do you expect to have happen next after Line 9 executes?
 
Laura Tobin
Greenhorn
Posts: 18
Eclipse IDE Java Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At runtime I get this:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at bull.Args.main(Args.java:14)


It's supposed to let me type in 2 integers, like 2+3, or 5-1, etc; and check which operator to use, perform the math, and then return the output as a msg. So basically a calculator.
Line 9 was supposed to be further down but I moved it up when I was switching things around to see if it made any difference. It just prints a line saying "wrong number of arguments" and an 2 it's supposed to only go off if I try to add more than 2 ints.
 
Stevens Miller
Bartender
Posts: 1445
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Based on that error message, it appears you have a package statement at the top that isn't in your posted code (I use that package name myself, actually). That's skewed your line numbers as posted here. Can you repost with exactly the code you are running, please?
 
Laura Tobin
Greenhorn
Posts: 18
Eclipse IDE Java Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oope, that was the one I was trying to run just in the console as a standalone(which didn't work either.)

Here is the original.

 
Stevens Miller
Bartender
Posts: 1445
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the meantime, a couple of thoughts are these: It's clearly not getting your arguments. Are you running from the command line or from an IDE? How are you passing the arguments? And, are you putting any spaces between your numbers and your operators?
 
Stevens Miller
Bartender
Posts: 1445
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I compiled your code and from the command line, it works for me. How are you running it? That is, on the command line, exactly what are you typing?
 
Laura Tobin
Greenhorn
Posts: 18
Eclipse IDE Java Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried both and...Eclipse was never letting me put in the arguments. So I thought, I should be a grownup about this and get the console going. That is still misbehaving.
In the meantime, I went and looked up "passing arguments into Eclipse" and I'm gonna go crawl into a hole now... it was right there in "Run Configurations" the whole time.

It works when I do the numbers and operators with spaces. OMG.

 
Stevens Miller
Bartender
Posts: 1445
30
C++ Java Netbeans IDE Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Laura Tobin wrote:I tried both and...Eclipse was never letting me put in the arguments. So I thought, I should be a grownup about this and get the console going.


Okay, there are some lessons here to learn: First, when the error message can't possibly be right... it still is. What I mean is, when you are thinking, "How can I possibly be seeing an out-of-bounds exception for index 0 when I know there is something in my array at that index?" it is time to stop thinking that and start thinking, "How come my attempts to put something into my array at index 0 are not succeeding?"

Second, when you are learning the basics, don't try to solve problems by learning even more things. Not sure how an Eclipse console works (I speak NetBeans, myself), but if "be a grownup" means figuring out something you didn't already know, then the lesson there would be to stick with what you do you know until you fix your bug. (Obviously, that's not set in stone. For one thing, you will probably have to learn something new for every bug you fix. But, if you feel like you should be able to write a working program with the tools already at hand, try to stick with those until you do have a working program. Leverage your success by then learning how to apply a new tool to your working program. Small steps are best.)
 
Laura Tobin
Greenhorn
Posts: 18
Eclipse IDE Java Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, I think the lesson I've learned is to stick with the IDE I know and maybe just explore it more...I'm in the place where I can see when something's wrong but not what, and when I can see what I miss the why... but it's all part of learning isn't it? Thank you so much for your help.
 
Campbell Ritchie
Marshal
Posts: 56592
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would have found it easier had you not used the IDE.
When you R‑click the name of the class and go to run, you get a submenu with run configurations on. Go to run configurations and you get an option for arguments (JVM or command arguments). You want to add those arguments as command arguments.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!