I am trying to understand Java Code which gives an error on compiling that index out of bound. I am trying to figure out why it is giving this error but failed. Can anyone help me that why this code is giving index out of bound error?
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);
}
}
Here is the ProfilerServant Class with the constructor
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();
}
The entry point of a java application is the main
method.
public static void main(String[] args){}
args
is a String
array of the command line arguments used to run the program.
Assume your main class is Program.java
.
On the terminal or command prompt, compile the program with javac Program.java
and run with java Program /filename true
The args
array is: "java","Program","/filename","true"
Given ProfilerServant(String fileName, boolean cacheEnabled)
, you can instantiate ProfilerServant
as:
ProfilerServant profilerServant = new ProfilerServant(args[2],
args[3].equals("true"));
//This turns to:
ProfilerServant profilerServant = new ProfilerServant("/filename",
"true".equals("true"));
Attempting to access an index out of bounds of args
will result in IndexOutOfBounds
.
The code is passing args to profilerServant class constructor. make sure initialization of the values of args[4] and args[5] are correct. check the length of argument args[4].
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.