java - Object[] 声明中的 IndexOutOfBoundsException

标签 java indexoutofboundsexception

我正在尝试修复一个烦人的大型程序(我没有创建它)的代码,但我不断收到著名的 IndexOutOfBoundsException

我想提一下,我知道错误的含义,因为我已经广泛搜索了答案,但我只是不明白为什么我得到它,所以这里...

这里是错误:

java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(ArrayList.java:635)
at java.util.ArrayList.get(ArrayList.java:411)
at edu.harvard.i2b2.crc.loader.dao.ObservationFactDAO$ObservationFactInsert.insert(ObservationFactDAO.java:359)
at edu.harvard.i2b2.crc.loader.dao.ObservationFactInsertHandle.insertObservationFact(ObservationFactInsertHandle.java:25)
at edu.harvard.i2b2.crc.loader.ejb.ObservationFactXmlDbLoader.process(ObservationFactXmlDbLoader.java:84)
at edu.harvard.i2b2.crc.loader.xml.TypePullParser.doParsing(TypePullParser.java:73)...

这是导致问题的 ObservationFactDAO.java 部分(如错误所引用):

    protected void insert(ObservationType observationType) {

        Object[] objs = new Object[] {
                observationType.getEventId().getValue(),
                observationType.getEventId().getSource(),
                observationType.getConceptCd().getValue(),
                (observationType.getPatientId() != null) ? observationType
                        .getPatientId().getValue() : null,
                (observationType.getPatientId() != null) ? observationType
                        .getPatientId().getSource() : null,
                (observationType.getObserverCd() != null) ? observationType
                        .getObserverCd().getValue() : null,
                (observationType.getStartDate() != null) ? observationType
                        .getStartDate().toGregorianCalendar().getTime()
                        : null,
                (observationType.getModifierCd() != null) ? observationType
                        .getModifierCd().getValue() : null,
                (observationType.getInstanceNum() != null) ? observationType
                        .getInstanceNum().getValue()
                        : null,
                observationType.getValuetypeCd(),
                observationType.getTvalChar(),
                (observationType.getNvalNum() != null) ? observationType
                        .getNvalNum().getValue() : null,
                (observationType.getValueflagCd() != null) ? observationType
                        .getValueflagCd().getValue()
                        : null,
                (observationType.getQuantityNum() != null) ? observationType
                        .getQuantityNum()
                        : null,

                null,
                (observationType.getObservationBlob() != null) ? observationType
                        .getObservationBlob().getContent().get(0)
                        .toString()
                        : null,
                observationType.getUnitsCd(),
                (observationType.getEndDate() != null) ? observationType
                        .getEndDate().toGregorianCalendar().getTime()
                        : null,
                (observationType.getLocationCd() != null) ? observationType
                        .getLocationCd().getValue() : null,
                (observationType.getUpdateDate() != null) ? observationType
                        .getUpdateDate().toGregorianCalendar().getTime()
                        : null,
                (observationType.getDownloadDate() != null) ? observationType
                        .getDownloadDate().toGregorianCalendar().getTime()
                        : null,
                (observationType.getImportDate() != null) ? observationType
                        .getImportDate().toGregorianCalendar().getTime()
                        : null,
                observationType.getSourcesystemCd(),
                observationType.getUploadId() };
        update(objs);
    }

更具体地说,引用了第一行 Object[] objs = new Object[]

如您所见,列表正在此处定义,所以我不明白为什么会在此处发送异常。

对象声明中调用的方法是有效且简单的“返回”语句。此外,我尝试用具有相同结果的实际值替换它们。

我仍在研究,但大多数关于此异常的现有帖子都关注向现有空列表添加值而不是其初始化。

最佳答案

问题不在于数组声明,它根本无法抛出 IndexOutOfBoundsException,而在于您正在访问的 ArrayList 以获得元素之一放入您正在创建的数组。具体来说:

(observationType.getObservationBlob() != null) ? observationType
    .getObservationBlob().getContent().get(0)
    .toString()
    : null

如果 getObservationBlob().getContent() 为空,get(0) 将抛出此异常,因此您应该明确检查它:

(observationType.getObservationBlob() != null && 
 observationType.getObservationBlob().getContent() != null &&
 !observationType.getObservationBlob().getContent().isEmpty()) ? 
    observationType.getObservationBlob().getContent().get(0).toString()
    : null

关于java - Object[] 声明中的 IndexOutOfBoundsException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37878750/

相关文章:

java - 从现有 Excel 中提取具有 2 个精度值 (3.55) 的特定列。帮我制作一下,它有1个精度值(3.5,3.9)

java - 索引越界异常: getting same row repeated several times

java - 如何修复合并排序方法的 ArrayIndexOutOfBoundsException?

java - JFreeChart 注释

java - 从数组中获取随机且不重复的对象

java - 如何在 Android 上升级 Apache HttpClient 版本

java - 通过 Twitter OAuth 登录不记得授权

c# - 更改数组中索引的值,将从旧索引开始的索引向右移动,同时保留左侧的所有索引不变

android - 我需要以输入字符串中的数字开头的字符串

java - 这个程序从哪里获取它的数字,为什么这是由于增加 1 数组大小引起的? ( java )