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

2
  • 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. Commented Jan 6, 2010 at 6:23
  • 1
    Show us the definitions and initializations of args and dataToPass. Commented Jan 6, 2010 at 6:39

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

Comments

2

You are not passing an argument to your program.

Comments

2

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

Comments

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?

2 Comments

In java, args[0] is not the program name.
@Jerome, then his problem is (as others said) that he is not passing any parameter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.