0

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

Cœur
  • 37,241
  • 25
  • 195
  • 267
Bharath
  • 665
  • 5
  • 12
  • 20
  • 3
    The first step in figuring out what the problem is is to identify the line that is triggering the exception. The stack that is printed with the exception should point you to the line that is causing your headache. – akf Jan 06 '10 at 06:23
  • 1
    Show us the definitions and initializations of `args` and `dataToPass`. – Jerome Jan 06 '10 at 06:39

4 Answers4

3

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
  • 349
  • 2
  • 14
2

You are not passing an argument to your program.

Chandra Patni
  • 17,347
  • 10
  • 55
  • 65
2

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

ojrac
  • 13,231
  • 6
  • 37
  • 39
2

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
  • 69,564
  • 10
  • 76
  • 117