Buggy code:
public class InstantiatedIndexReader extends IndexReader { private final InstantiatedIndex index ; private ReaderContext context = new AtomicReaderContext ( this ) ; public InstantiatedIndexReader ( InstantiatedIndex index ) { } @ Override public boolean isOptimized ( ) { } @ Override public long getVersion ( ) { } @ Override public Directory directory ( ) { } @ Override public boolean isCurrent ( ) throws IOException { } public InstantiatedIndex getIndex ( ) { } @ Override public Bits getLiveDocs ( ) { } private BitVector uncommittedDeletedDocuments ; private Map < String , List < InstantiatedIndexReader . NormUpdate > > uncommittedNormsByFieldNameAndDocumentNumber = null ; private static class NormUpdate { private int doc ; private byte value ; public NormUpdate ( int doc , byte value ) { } } @ Override public int numDocs ( ) { } @ Override public int maxDoc ( ) { } @ Override public boolean hasDeletions ( ) { } @ Override protected void doDelete ( int docNum ) throws IOException { } @ Override protected void doUndeleteAll ( ) throws IOException { } @ Override protected void doCommit ( Map < String , String > commitUserData ) throws IOException { } @ Override protected void doClose ( ) throws IOException { } @ Override public Collection < String > getFieldNames ( FieldOption fieldOption ) { } @ Override public Document document ( int n , FieldSelector fieldSelector ) throws IOException , CorruptIndexException { } @ Override public Document document ( int n ) throws IOException { } @ Override public byte [ ] norms ( String field ) throws IOException { } @ Override protected void doSetNorm ( int doc , String field , byte value ) throws IOException { } @ Override public int docFreq ( Term t ) throws IOException { } @ Override public Fields fields ( ) { if ( ( getIndex ( ) . getOrderedTerms ( ) . length ) == 0 ) { return null ; } return new Fields ( ) { @ Override public FieldsEnum iterator ( ) { final InstantiatedTerm [ ] orderedTerms = getIndex ( ) . getOrderedTerms ( ) ; return new FieldsEnum ( ) { int upto = - 1 ; String currentField ; @ Override public String next ( ) { do { ( upto ) ++ ; if ( ( upto ) >= ( orderedTerms . length ) ) { return null ; } } while ( orderedTerms [ upto ] . field ( ) . equals ( currentField ) ) ; currentField = orderedTerms [ upto ] . field ( ) ; return currentField ; } @ Override public TermsEnum terms ( ) { return new InstantiatedTermsEnum ( orderedTerms , upto , currentField ) ; } } ; } @ Override public Terms terms ( final String field ) { final InstantiatedTerm [ ] orderedTerms = getIndex ( ) . getOrderedTerms ( ) ; int i = Arrays . binarySearch ( orderedTerms , new Term ( field ) , termComparator ) ; if ( i < 0 ) { i = ( - i ) - 1 ; } if ( ( i >= ( orderedTerms . length ) ) || ( ! ( orderedTerms [ i ] . field ( ) . equals ( field ) ) ) ) { return null ; } final int startLoc = i ; long sum = 0 ; int upto = i ; <START_BUG> while ( ( upto < ( orderedTerms . length ) ) && ( orderedTerms [ i ] . equals ( field ) ) ) { <END_BUG> sum += orderedTerms [ i ] . getTotalTermFreq ( ) ; upto ++ ; } final long sumTotalTermFreq = sum ; return new Terms ( ) { @ Override public TermsEnum iterator ( ) { return new InstantiatedTermsEnum ( orderedTerms , startLoc , field ) ; } @ Override public long getSumTotalTermFreq ( ) { return sumTotalTermFreq ; } @ Override public Comparator < BytesRef > getComparator ( ) { return BytesRef . getUTF8SortedAsUnicodeComparator ( ) ; } } ; } } ; } @ Override public ReaderContext getTopReaderContext ( ) { } @ Override public TermFreqVector [ ] getTermFreqVectors ( int docNumber ) throws IOException { } @ Override public TermFreqVector getTermFreqVector ( int docNumber , String field ) throws IOException { } @ Override public void getTermFreqVector ( int docNumber , String field , TermVectorMapper mapper ) throws IOException { } @ Override public void getTermFreqVector ( int docNumber , TermVectorMapper mapper ) throws IOException { } @ Override public PerDocValues perDocValues ( ) throws IOException { } }

Fix:
while ( ( upto < ( orderedTerms . length ) ) && ( orderedTerms [ i ] . field ( ) . equals ( field ) ) ) {