String conversion in java is throwing array index

2019-08-23 04:07发布

问题:

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

回答1:

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.



回答2:

You are not passing an argument to your program.



回答3:

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



回答4:

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?