我在下面的代码中得到了运行时异常ArrayIndexOutOfBoundException
,在行中:
if ( args[0].equals("t") || args[0].equals("time") )
。
请帮助我。
public static void main(String[] args) {
System.out.println("Starting Test Cases");
try
{
// Parse the command line args
int policy = 0;
if ( args[0].equals("t") || args[0].equals("time") ) {
policy = ResourceCharacteristics.TIME_SHARED`enter code here`;
}
else if ( args[0].equals("s") || args[0].equals("space") ) {
policy = ResourceCharacteristics.SPACE_SHARED;
}
else {
System.out.println("Error -- Invalid allocation policy....");
return;
}
// determine which test case number to choose
int testNum = Integer.parseInt(args[1]);
if (testNum < MIN || testNum > MAX) {
testNum = MIN;
}
////////////////////////////////////////
// First step: Initialize the GridSim package. It should be called
// before creating any entities. We can't run this example without
// initializing GridSim first. We will get run-time exception
// error.
Calendar calendar = Calendar.getInstance();
boolean trace_flag = false; // true means tracing GridSim events
// list of files or processing names to be excluded from any
// statistical measures
String[] exclude_from_file = { "" };
String[] exclude_from_processing = { "" };
// the name of a report file to be written. We don't want to write
// anything here.
String report_name = null;
// initialize all revelant variables
double baudRate[] = {1000, 5000}; // bandwidth for even, odd
int peRating[] = {10, 50}; // PE Rating for even, odd
double price[] = {3.0, 5.0}; // resource for even, odd
int gridletLength[] = {1000, 2000, 3000, 4000, 5000};
// Initialize the GridSim package
int totalUser = 2; // total Users for this experiment
GridSim.init(totalUser, calendar, trace_flag, exclude_from_file,
exclude_from_processing, report_name);
//////////////////////////////////////
// Second step: Creates one or more GridResource objects
int totalResource = 3; // total GridResources for this experiment
int totalMachine = 1; // total Machines for each GridResource
int totalPE = 3; // total PEs for each Machine
createResource(totalResource, totalMachine, totalPE, baudRate,
peRating, price, policy);
/////////////////////////////////////
// Third step: Creates grid users
int totalGridlet = 4; // total Gridlets for each User
createUser(totalUser, totalGridlet, gridletLength, baudRate,
testNum);
////////////////////////////////////
// Fourth step: Starts the simulation
GridSim.startGridSimulation();
}
catch (Exception e)
{
System.out.println("Unwanted errors happen");
System.out.println( e.getMessage() );
System.out.println("Usage: java Test [time | space] [1-8]");
}
System.out.println("=============== END OF TEST ====================");
}
当它被执行时,它将抛出异常并且始终执行catch块。
答案 0 :(得分:3)
这意味着在没有命令行或IDE模拟参数的情况下调用您的程序,因此args
的大小为0
,引用索引0
将引发ArrayIndexOutOfBoundsException
。
如果您从命令行运行此命令,请添加程序参数(例如java MyMainClass time
)。
如果您在IDE中运行,则可以为运行配置添加参数。
在Eclipse中:
Run/Debug settings
Arguments
标签program arguments
注意强>
一个好的做法是在引用其任何元素之前检查任何数组的大小,例如: if (args.length > 0) {...}
答案 1 :(得分:1)
您首先需要检查是否有任何 :
if (args.length == 0) {
System.out.println("Error -- Allocation policy not specified.");
return;
}
// other checks you currently have left as is
答案 2 :(得分:-1)
您的程序还应该处理负面情况(以便不会抛出索引超出范围的异常)。检查以查看输入参数的长度,并应正确地发送消息/日志以指示输入是必需的。