【发布时间】:2016-06-15 10:27:50
【问题描述】:
我查看了有关此异常的所有问题。我知道数组索引从0开始,如果大小为1,我们只能访问索引0。虽然我知道这个逻辑,但我无法修复以下代码。
private static Map<String, Map<String, List<String>>> executions = new HashMap<String, Map<String, List<String>>>();
public static String readExecution(String jobName, String deviceName, int executionNumber) {
String result = null;
Map<String, List<String>> jobExecutions = executions.get(jobName);
if (jobExecutions != null) {
List<String> executionPerDevice = jobExecutions.get(deviceName);
result = executionPerDevice.get(executionNumber); // ERROR
}
return result;
}
编辑: Fildor的comment.execution number的答案是1。
【问题讨论】:
-
调试它。我会说
executionNumber是“1”。如果您从 1 开始对执行进行编号,则将其设为result = executionPerDevice.get(executionNumber-1);我还会进行一些空值检查。 -
你能检查 executionPerDevice 的大小并将其与 executionNumber 进行比较吗?
-
IndexOutOfBound 异常是一个非常简单的异常。你试图得到一些不存在的东西。只需调试您的代码,找出为什么在获取它时没有价值。
-
如果
deviceName不在jobExecutions 中会怎样? (反问) -
你有它。因此,您实际上是在尝试在其大小为 1 时执行
executionPerDevice.get(1)。您已经说过您知道这是错误的。索引应为 0。请参阅我的第一条评论。
标签: java indexoutofboundsexception