views:

99

answers:

2

Hi All,

I have written following java code. It is throwing array index out of range exception

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

Can some resolve this issue plz

public class UnifiedClus1toNfastfastsamehost extends UnifiedClus1toNfastfastsamehostHelper
{
    /**
     * Script Name   : <b>UnifiedClus1toNfastfastsamehost</b>
     * Generated     : <b>Aug 3, 2007 1:16:35 AM</b>
     * Description   : Functional Test Script
     * Original Host : WinNT Version 5.1  Build 2600 (S)
     * 
     * @since  2007/08/03
     * @author Administrator
     */

    String[] dataToPass = new String[1];
    public void testMain(Object[] args) 
    {
        String options = "" + args[0];

        callScript("Cleanup");
        functions.formatall();

        dataToPass[0]= "resyncdatagen";
        callScript("Clus1toNfastfastsamehost",dataToPass);

        dataToPass[0]= "configurepair1";
        callScript("Clus1toNfastfastsamehost",dataToPass);

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

        dataToPass[0]= "WFE1";
        callScript("Clus1toNfastfastsamehost",dataToPass);

        dataToPass[0]= "configurepair2";
        callScript("Clus1toNfastfastsamehost",dataToPass);

        dataToPass[0]= "WFE2";
        callScript("Clus1toNfastfastsamehost",dataToPass);
        sleep(180);

        dataToPass[0]= "vsnap1";
        callScript("Clus1toNfastfastsamehost",dataToPass);

        dataToPass[0]= "dataverf1";
        callScript("Clus1toNfastfastsamehost",dataToPass);

    /*  if (options.toLowerCase().contains("failover"))
        {
            dataToPass[0]= "diffdatagen1fover";
            callScript("Clus1toNfastfastsamehost",dataToPass);
        }   
        else
        if (options.toLowerCase().contains("normal"))
        {
            dataToPass[0]= "diffdatagen1normal";
            callScript("Clus1toNfastfastsamehost",dataToPass);
        }

        dataToPass[0]= "vsnap2";
        callScript("Clus1toNfastfastsamehost",dataToPass);

        dataToPass[0]= "dataverf2";
        callScript("Clus1toNfastfastsamehost",dataToPass);

        dataToPass[0]= "clean";
        callScript("Clus1toNfastfastsamehost",dataToPass);

        dataToPass[0]= "formatallsource";
        callScript("Clus1toNfastfastsamehost",dataToPass);

        if (options.toLowerCase().contains("failover"))
        {
            dataToPass[0]= "formatallclusfover";
            callScript("Clus1toNfastfastsamehost",dataToPass);
        }   
        else
        if (options.toLowerCase().contains("normal"))
        {
            dataToPass[0]= "formatallclusnormal";
            callScript("Clus1toNfastfastsamehost",dataToPass);
        }*/
    }
}
A: 

Check that while making call to testMain(Object[] args) the param "args" should not be null. Better put a null check in the method itself

String options = ""; if (args!=null){ options = options+ args[0]; }

Ravi Gupta
`-1` for being completely offtopic as the exception is not thrown from the shown code, and another `-1` for blindly suggesting `StringBuilder` because two `String` instances were concatenated *once*.
Bombe
Interestingly enough, "" + `null` yields the string `"null"`. Perhaps not as intended, but it won't explode. @Bombe: Agree completely on 2nd criticism, it is downright WRONG to replace simple concatenation with SB. Zillions of Java programmers don't understand when SB is appropriate.
Carl Smotricz
Yep, got it, but some details were missing from the question itself.
Ravi Gupta
+1  A: 

It's unfortunate your exception doesn't show source file or line number, this leaves us to guess.

I don't see any use of a subscript 1 in the code shown, so the problem is likely in one of the called methods.

if (options.toLowerCase().contains("Failover"))

contains a bug, though: Once you lowercase options, the resulting String will not contain a capital "F" as in "Failover"!

Carl Smotricz