繁体   English   中英

具有链接列表的索引0大小为0的IndexOutOFBound异常

[英]IndexOutOFBound exception with index 0 size 0 with linked list

 java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
    at java.util.LinkedList.checkElementIndex(LinkedList.java:553)
    at java.util.LinkedList.get(LinkedList.java:474)
    at com.comcast.guide.actionhandlers.MiniGuideActions.checkChannelFocus(MiniGuideActions.java:79)
    at com.comcast.guide.functests.MiniGuideTest.checkChannelFocus(MiniGuideTest.java:34)`

我看到这行有一个错误

String currentAiringChannelNumber=moduleModel.getGenerators().get(0).getStringParam(TP_CHANNEL_NUMBER)== null ? "" : moduleModel.getGenerators().get(0).getStringParam(TP_CHANNEL_NUMBER);

E获取方法:

public E get(int index) {
       checkElementIndex(index);
         return node(index).item;
}

checkElementIndex方法:

private String outOfBoundsMsg(int index) {
     return "Index: "+index+", Size: "+size;
}
private void checkElementIndex(int index) {
    if (!isElementIndex(index))
        throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
private void checkPositionIndex(int index) {
    if (!isPositionIndex(index))
        throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}

我是Java的新手,我无法弄清楚应该在哪里更改代码。 尽管测试用例是按照流程执行的,但它最终会返回此异常,并在最后给出异常消息。 有人可以帮我解决吗?

这意味着尽管LinkedList没有对象(其size为0),但您正在尝试访问index 0的对象(因此是第一个对象)。 因此,您需要在尝试访问对象之前添加一个对象。

您可以在尝试访问其中的对象之前,检查list.size() > 0LinkedList是否存在任何对象。

您需要检查LinkedList并确保已填充它。 下面是一个示例,我将其分解为多个if语句,以使其更具可读性。

String currentAiringChannelNumber = "";
if (moduleModel.getGenerators() != null && moduleModel.getGenerators().size() > 0) {
    currentAiringChannelNumber = moduleModel.getGenerators().get(0).getStringParam(TP_CHANNEL_NUMBER) == null ? "" : moduleModel.getGenerators().get(0).getStringParam(TP_CHANNEL_NUMBER);
}

您在“ getGenerators()”中没有元素。

您可能必须验证元素的存在。

试试这个

String currentAiringChannelNumber=moduleModel.getGenerators().isElementIndex(0) ? "" : moduleModel.getGenerators().get(0).getStringParam(TP_CHANNEL_NUMBER);

似乎getGenerator()在您的代码中返回了一个列表。 在此列表上使用get(0)之前,需要确保其大小应大于0。

您可以使用try-catch,因为它可以处理引发的异常。

使用以下代码:

    String currentAiringChannelNumber; //Declare String object
    try{ // Following block of code is ran, if an exception is thrown it will be caught and the the lines of code in the try block after the line of code that caused the exception to be thrown will not be run instead the code in the catch block will be ran.
        currentAiringChannelNumber = moduleModel.getGenerators().get(0).getStringParam(TP_CHANNEL_NUMBER);
    }
    catch(Exception e){ //Catching any exception thrown, accommodates for NullPointerException, IndexOutOfBoundsException as well as all other exceptions that are thrown. 
        currentAiringChannelNumber  = ""; //Code to handle predicted exceptions being thrown, which are the NullPointerException and IndexOutOfBoundsException.
    }

注意:try-catch语句之后的代码将运行,无论在try块中执行代码时是否引发异常。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM