Object []声明中的IndexOutOfBoundsException

Rom*_* B. 2 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)...
Run Code Online (Sandbox Code Playgroud)

以下是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);
    }
Run Code Online (Sandbox Code Playgroud)

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

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

Object声明中调用的方法是有效且简单的'return'语句.此外,我尝试用实际值替换它们,结果相同.

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

Mur*_*nik 5

问题不在于数组声明,它只是不能抛出一个IndexOutOfBoundsException,但是ArrayList你正在访问它以便将一个元素放入你正在创建的数组中.特别:

(observationType.getObservationBlob() != null) ? observationType
    .getObservationBlob().getContent().get(0)
    .toString()
    : null
Run Code Online (Sandbox Code Playgroud)

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

(observationType.getObservationBlob() != null && 
 observationType.getObservationBlob().getContent() != null &&
 !observationType.getObservationBlob().getContent().isEmpty()) ? 
    observationType.getObservationBlob().getContent().get(0).toString()
    : null
Run Code Online (Sandbox Code Playgroud)