2010-01-06 6 views
0

J'ai écrit le code java suivant. Il jette l'index de tableau hors exception gammeconversion de chaîne java

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

une résolution peut cette question 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); 
     }*/ 
    } 
} 
+0

Veuillez publier toute l'exception avec trace de pile. –

+1

Duplicata de http://stackoverflow.com/questions/2011247/string-convertion-in-java? ou cela peut être une information supplémentaire qui était destinée à cette autre question. Peut-être cela a besoin d'aide de modération? –

+0

Options de chaîne = "" + args [0]; Pourquoi l'utilisez-vous? Vous pouvez simplement le convertir en String, si (args [0] instanceof String) String options = (String) args [0]; . Et Kevin + 1. –

Répondre

0

Vérifiez que tout en faisant appel à testMain (Object [] args) le PARAM "args" ne devrait pas être nul. Il vaut mieux mettre une vérification nulle dans la méthode elle-même

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

+3

'-1' pour être complètement offtopic car l'exception n'est pas levée du code montré, et un autre' -1' pour suggérer aveuglément 'StringBuilder' car deux instances' String' ont été concaténées * une fois *. – Bombe

+0

Fait intéressant, "" + "null" donne la chaîne '" null "'. Peut-être pas comme prévu, mais ça ne va pas exploser. @Bombe: Tout à fait d'accord sur la 2ème critique, il est carrément MAUVAIS de remplacer la simple concaténation par SB. Zillions de programmeurs Java ne comprennent pas quand SB est approprié. –

+0

Oui, j'ai compris, mais certains détails manquaient à la question elle-même. –

1

Il est malheureux que votre exception ne montre pas le fichier source ou le numéro de ligne, cela nous laisse deviner.

Je ne vois aucune utilisation d'un indice 1 dans le code affiché, donc le problème est probable dans l'une des méthodes appelées.

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

contient un bug, si: Une fois que vous en minuscule options, la chaîne résultante ne contiendra pas un capital "F" comme dans "Failover"!