aspose file tools*
The moose likes Java in General and the fly likes Exception in thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 0" Watch "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 0" New topic
Author

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

Sakthi velan
Greenhorn

Joined: May 12, 2011
Posts: 3
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

Joined: Aug 16, 2005
Posts: 14338
    
  22

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.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
vibhor sharma
Greenhorn

Joined: Dec 10, 2010
Posts: 19


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

Joined: May 12, 2011
Posts: 3
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

Joined: Dec 10, 2010
Posts: 19

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

Joined: May 12, 2011
Posts: 3
Thanks a lot. It worked.
umesh mahato
Greenhorn

Joined: Nov 19, 2012
Posts: 1
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

Joined: Sep 04, 2012
Posts: 81
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

Joined: Apr 06, 2013
Posts: 43
@Jesper de Jong

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Apr 06, 2013
Posts: 43
i am not using IDE .. just text editor .. i meant some theory to help understand the problem.
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
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

Joined: Apr 06, 2013
Posts: 43
@Stuart A. Burkett

thanks .. it's makes sense now .. cheers
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 0