2016-06-17 5 views
2

私は面倒な大きなプログラムのコードを修正しようとしています(作成しませんでした)、有名人の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[]が参照されます。

ご覧のとおり、リストはここで定義されているので、ここで例外が送信される理由はわかりません。

オブジェクト宣言で呼び出されるメソッドは、有効で単純な 'return'ステートメントです。さらに、私は同じ結果を実際の値に置き換えようとしました。

私はまだ研究中ですが、この例外に関する既存の記事のほとんどは、既存の空のリストに値を追加し、その初期化を行わないことを懸念しています。

+1

私は、ひどい探しの配列初心者を書き換えて、少なくとも読むことができるようにしたいと思っています。 –

+2

配列リストがエラーをスローしています。 Object []とは何の関係もありません。 – matt

答えて

5

問題は配列宣言ではなく、IndexOutOfBoundsExceptionを投げることはできませんが、作成中の配列に要素の1つを入れるためにアクセスしているのはArrayListです。例外のトレースがはっきりObservationFactDAO.javaの359は、getを呼び出して、その行を示している

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

あなたの答えをありがとう、私は今なぜ例外を得る。あなたのソリューションは適切だと思われますが、 'isEmpty()'関数のための特別なインポートがありますか?コンパイルでエラーが発生する –

+0

特に 'シンボルが見つかりません' –

+0

@RomainB。私の間違い - あなたは 'getContent()'によって返されたArrayListに 'isEmpty()'を適用することになっています - 私の編集された答えを見てください。 – Mureinik

1

:具体的に:getObservationBlob().getContent()が空である

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

場合は、明示的にそれに対してチェックする必要がありますので、get(0)は、この例外をスローしますarraylistには(0)があり、インデックス0には要素がありません。つまり、この時点でarraylistは空です。

データの問題やロジック上の問題により、このコードポイントにはデータが配列されていることが予想されるためです。

+0

これは空の配列ではなく、 'ArrayList'です(スタックトレースに明記されています)。 – Hulk

+0

ご清聴ありがとうございます。 –

関連する問題