-1

i am using this program and getting the above error!

public class Src {

    public static void main(String args[]) {
        // TODO Auto-generated method stub
        int a=Integer.parseInt(args[0]);
        int b=Integer.parseInt(args[1]);
        int c=a+b;
        System.out.println(""+c);
    }
}
Am_I_Helpful
  • 18,735
  • 7
  • 49
  • 73

2 Answers2

2

You have to pass some arguments at the command line, when you call the program:

java Src arg0 arg1

args is a String[] that gets filled with the command-line values passed when invoking the program, by default it's empty, hence an ArrayIndexOutOfBoundsException is thrown because there's nothing there at index 0.

Óscar López
  • 232,561
  • 37
  • 312
  • 386
2

The problem is that you are trying to read the first element in the args[] array and this element doesn't exist.

The args[] array contains arguments passed during the start of the program in the command line, maybe you're not passing argument.

You can find an example of ArrayIndexOutOfBoundException here: https://stackoverflow.com/a/5554781/2649618

This is the official documentation page on Oracle website (is for Java 7): http://docs.oracle.com/javase/7/docs/api/java/lang/ArrayIndexOutOfBoundsException.html

Community
  • 1
  • 1
Ema.jar
  • 2,370
  • 1
  • 33
  • 43