• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

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

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.


 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 19
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot. It worked.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ..
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am not using IDE .. just text editor .. i meant some theory to help understand the problem.
 
Ranch Hand
Posts: 679
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Stuart A. Burkett

thanks .. it's makes sense now .. cheers
 
There's a hole in the bucket, dear Liza, dear Liza, a hole in the bucket, dear liza, a tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic