Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 0

 
Sakthi velan
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Getting an ArrayIndexOutOfBoundsException.
I am just trying to get input of two values.
Below is my code. Can any one please help me.


 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15207
36
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

How are you running this program? It expects you to supply two numbers on the command line when you run it. You should for example type in a command like this:

java Test 3 4

If you don't supply the numbers, then args will be an array of length 0 and you will get an ArrayIndexOutOfBoundsException in line 13.
 
vibhor sharma
Greenhorn
Posts: 19
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

follow what jasper says:
as well , change your line 16 , such as

showresult("Reult is " + execute(op1, op2));

otherwise you will get the String representation [L@3e... , instead of getting numeric literal.
 
Sakthi velan
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the immediate reply.
I am using Eclipse IDE to run it. How will I give the inut parameters in that?
 
vibhor sharma
Greenhorn
Posts: 19
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
go to project explorer and select your project , right click on it and you will find

Run as->Run configurations->arguments -> program arguments

provide your values in the window shown, be careful to put a space between your values , apply and run

That's it !
 
Sakthi velan
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot. It worked.
 
umesh mahato
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i'm using netbeans ide 7.3 beta for java programming. i'll be grateful to anyone there who could help me with the same error in netbeans.
 
Wesleigh Pieters
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
umesh mahato wrote:i'm using netbeans ide 7.3 beta for java programming. i'll be grateful to anyone there who could help me with the same error in netbeans.


if you right click the project and select properties, under the run options there is arguments.
 
Abid Ramay
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Jesper de Jong

your suggestin worked but can't make no sense .. if you point in the direction will be helpfull ..
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abid Ramay wrote:@Jesper de Jong

your suggestin worked but can't make no sense .. if you point in the direction will be helpfull ..


He explained it pretty clearly. What don't you understand?
 
Abid Ramay
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am not using IDE .. just text editor .. i meant some theory to help understand the problem.
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abid Ramay wrote:i meant some theory to help understand the problem.

The problem was that if you don't pass any command line arguments to a program, the args array that is a parameter to the main method will not contain any values - it will be a zero lenghth array.
On line 13 of the program an attempt was made to access the first element of the array. Because the array has no elements, this isn't possible so the program throws an ArrayIndexOutOfBoundsException.

In general, if an array has size n, you can only access elements with indexes less than n and positive
e.g.
If you have an array of size 3, you can access elements 0, 1 and 2

 
Abid Ramay
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Stuart A. Burkett

thanks .. it's makes sense now .. cheers
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic