在Corba应用程序中索引超出范围的错误

时间:2015-09-26 01:13:47

标签: java

我正在尝试理解Java Code,它在编译索引超出范围时会出错。我试图弄清楚为什么它会给出这个错误但是失败了。任何人都可以帮助我,为什么这段代码给出了索引超出范围的错误?

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();
}

2 个答案:

答案 0 :(得分:2)

java应用程序的入口点是main方法。

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

args是用于运行程序的命令行参数的String数组。

假设您的主要班级是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 :(得分:-1)

代码将args传递给profilerServant类构造函数。 确保args [4]和args [5]的值的初始化是正确的。 检查参数args的长度[4]。

相关问题