views:

117

answers:

4

Hi All, i have written following code but it is throwing array index out of range exception

    String options = "" + args[0];

    if (options.toLowerCase().contains("failover"))
    {
        dataToPass[0]= "failover";
        callScript("Clus1toNfastfastsamehost",dataToPass);
    }

Exceptions: exception_name = java.lang.ArrayIndexOutOfBoundsException exception_message = Array index out of range: 1

Can some resolve this issue.

+1  A: 

You are not passing an argument to your program.

Chandra Patni
+1  A: 

Well, it's a straightforward exception. Check all your array lengths. How many items are in args? In dataToPass? Consider using a debugger.

ojrac
+1  A: 

UPDATE WITH CODE FIX

String options = ""
if (args.length > 0)
  options += args[0]

Original comments:

There are two places you reference an array in the example code. args[0] and dataToPass[0]

It must be one of those two. So, a) you are not passing any arguments to the programs and args[0] is not defined -- this seems strange to me because I thought args[0] was the program name or b) dataToPass[0] was not allocated -- is dataToPass a zero length array and not a 1 length array?

Hogan
theoretically, it could also be within the `callScript` method...
akf
In java, args[0] is not the program name.
Jerome
@Jerome, then his problem is (as others said) that he is not passing any parameter.
Hogan
+2  A: 

Well, Either you are not allocated enough memory the dataToPass[] or you have not pass arguments to the program. If no arguments is passed then args, it's a zero length array. Debugging will be a good option for you mate.

Nitin Ware