0

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);
        }*/
    }
}
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
Bharath
  • 665
  • 5
  • 12
  • 20
  • Please post the whole exception with stack trace. – Kevin Brock Jan 06 '10 at 06:40
  • 1
    Duplicate of http://stackoverflow.com/questions/2011247/string-convertion-in-java? or this may be additional information that was intended for that other question. Perhaps this needs some moderation assistance? – Kevin Brock Jan 06 '10 at 06:42
  • String options = "" + args[0]; Why do you use this ? You could just cast it to String, if(args[0] instanceof String) String options = (String) args[0]; . And Kevin+1. – Erkan Haspulat Jan 06 '10 at 06:53

2 Answers2

1

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
  • 66,391
  • 18
  • 125
  • 167
0

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
  • 4,468
  • 12
  • 54
  • 85
  • 3
    `-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 Jan 06 '10 at 07:31
  • 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 Jan 06 '10 at 07:59
  • Yep, got it, but some details were missing from the question itself. – Ravi Gupta Jan 06 '10 at 09:21