String conversion in java is throwing array index

2019-08-23 03:13发布

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

4条回答
爷、活的狠高调
2楼-- · 2019-08-23 03:49

You are not passing an argument to your program.

查看更多
混吃等死
3楼-- · 2019-08-23 03:53

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

查看更多
别忘想泡老子
4楼-- · 2019-08-23 04:07

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.

查看更多
等我变得足够好
5楼-- · 2019-08-23 04:14

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?

查看更多
登录 后发表回答