2015-09-26 46 views
-2

我想了解Java代碼,該代碼在編譯該索引時出錯。我試圖找出它爲什麼會給出這個錯誤,但失敗了。任何人都可以幫助我,爲什麼這段代碼給索引出界錯誤?在Corba應用程序中出現索引超出限制的錯誤

try { 
      ORB orb = ORB.init(args, null); 
      POA rootpoa = POAHelper.narrow(orb 
        .resolve_initial_references("RootPOA")); 
      rootpoa.the_POAManager().activate(); 

      ProfilerServant profilerServant = new ProfilerServant(args[4], 
        args[5].equals("true")); 
      org.omg.CORBA.Object ref = rootpoa 
        .servant_to_reference(profilerServant); 
      Profiler pref = ProfilerHelper.narrow(ref); 

      org.omg.CORBA.Object objRef = orb 
        .resolve_initial_references("NameService"); 
      NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef); 

      String name = "Profiler"; 
      NameComponent path[] = ncRef.to_name(name); 
      ncRef.rebind(path, pref); 

      orb.run(); 
     } 

     catch (Exception e) { 
      System.err.println("ERROR: " + e.getMessage()); 
      e.printStackTrace(System.out); 
     } 
    } 

這裏是ProfilerServant類 與構造

public class ProfilerServant extends ProfilerPOA { 

boolean cacheEnabled; 

ServerParser parser; 
HashMap<String, Integer> songCache; 
HashMap<String, User> userCache; 

ProfilerServant(String fileName, boolean cacheEnabled) { 
    this.cacheEnabled = cacheEnabled; 
    parser = new ServerParser(fileName); 
    songCache = new HashMap<String, Integer>(); 
    userCache = new HashMap<String, User>(); 
    init(); 
} 
+1

你確定'args [4]'和'args [5]'在範圍內嗎? –

+0

不知道。我現在是Java ..它的所有ProfilerServant類。 。 – Natalia

+0

args []的範圍是什麼.. – Natalia

回答

2

一個Java應用程序的入口點是main方法。

public static void main(String[] args){} 

argsString陣列的用於運行該程序的命令行參數。

假設您的主類是Program.java

在終端或命令提示,編譯程序用javac Program.java並用java Program /filename true

args陣列運行是:"java","Program","/filename","true"

鑑於ProfilerServant(String fileName, boolean cacheEnabled),可以實例化ProfilerServant爲:

ProfilerServant profilerServant = new ProfilerServant(args[2], 
       args[3].equals("true")); 
//This turns to: 
ProfilerServant profilerServant = new ProfilerServant("/filename", 
       "true".equals("true")); 

嘗試訪問args的索引超出範圍將導致IndexOutOfBounds

-1

代碼將args傳遞給profilerServant類構造函數。 確保args [4]和args [5]的值的初始化是正確的。 檢查參數參數的長度[4]。