Buggy code:
public class AssertingAtomicReader extends FilterAtomicReader { public AssertingAtomicReader ( AtomicReader in ) { } @ Override public Fields fields ( ) throws IOException { } @ Override public Fields getTermVectors ( int docID ) throws IOException { } public static class AssertingFields extends FilterFields { public AssertingFields ( Fields in ) { } @ Override public Iterator < String > iterator ( ) { } @ Override public Terms terms ( String field ) throws IOException { } } public static class AssertingTerms extends FilterTerms { public AssertingTerms ( Terms in ) { } @ Override public TermsEnum intersect ( CompiledAutomaton automaton , BytesRef bytes ) throws IOException { <START_BUG> TermsEnum termsEnum = super . intersect ( automaton , bytes ) ; <END_BUG> assert termsEnum != null ; assert ( bytes == null ) || ( bytes . isValid ( ) ) ; return new AssertingAtomicReader . AssertingTermsEnum ( termsEnum ) ; } @ Override public TermsEnum iterator ( TermsEnum reuse ) throws IOException { } } static class AssertingTermsEnum extends FilterTermsEnum { private enum State { INITIAL , POSITIONED , UNPOSITIONED ; } private AssertingAtomicReader . AssertingTermsEnum . State state = AssertingAtomicReader . AssertingTermsEnum . State . INITIAL ; public AssertingTermsEnum ( TermsEnum in ) { } @ Override public DocsEnum docs ( Bits liveDocs , DocsEnum reuse , int flags ) throws IOException { } @ Override public DocsAndPositionsEnum docsAndPositions ( Bits liveDocs , DocsAndPositionsEnum reuse , int flags ) throws IOException { } @ Override public BytesRef next ( ) throws IOException { } @ Override public long ord ( ) throws IOException { } @ Override public int docFreq ( ) throws IOException { } @ Override public long totalTermFreq ( ) throws IOException { } @ Override public BytesRef term ( ) throws IOException { } @ Override public void seekExact ( long ord ) throws IOException { } @ Override public SeekStatus seekCeil ( BytesRef term , boolean useCache ) throws IOException { } @ Override public boolean seekExact ( BytesRef text , boolean useCache ) throws IOException { } @ Override public TermState termState ( ) throws IOException { } @ Override public void seekExact ( BytesRef term , TermState state ) throws IOException { } } static enum DocsEnumState { START , ITERATING , FINISHED ; } static class AssertingDocsEnum extends FilterDocsEnum { private AssertingAtomicReader . DocsEnumState state = AssertingAtomicReader . DocsEnumState . START ; private int doc ; public AssertingDocsEnum ( DocsEnum in ) { } @ Override public int nextDoc ( ) throws IOException { } @ Override public int advance ( int target ) throws IOException { } @ Override public int freq ( ) throws IOException { } } static class AssertingDocsAndPositionsEnum extends FilterDocsAndPositionsEnum { private AssertingAtomicReader . DocsEnumState state = AssertingAtomicReader . DocsEnumState . START ; private int positionMax = 0 ; private int positionCount = 0 ; private int doc ; public AssertingDocsAndPositionsEnum ( DocsAndPositionsEnum in ) { } @ Override public int nextDoc ( ) throws IOException { } @ Override public int advance ( int target ) throws IOException { } @ Override public int freq ( ) throws IOException { } @ Override public int nextPosition ( ) throws IOException { } @ Override public int startOffset ( ) throws IOException { } @ Override public int endOffset ( ) throws IOException { } @ Override public BytesRef getPayload ( ) throws IOException { } } public static class AssertingNumericDocValues extends NumericDocValues { private final NumericDocValues in ; private final int maxDoc ; public AssertingNumericDocValues ( NumericDocValues in , int maxDoc ) { } @ Override public long get ( int docID ) { } } public static class AssertingBinaryDocValues extends BinaryDocValues { private final BinaryDocValues in ; private final int maxDoc ; public AssertingBinaryDocValues ( BinaryDocValues in , int maxDoc ) { } @ Override public void get ( int docID , BytesRef result ) { } } public static class AssertingSortedDocValues extends SortedDocValues { private final SortedDocValues in ; private final int maxDoc ; private final int valueCount ; public AssertingSortedDocValues ( SortedDocValues in , int maxDoc ) { } @ Override public int getOrd ( int docID ) { } @ Override public void lookupOrd ( int ord , BytesRef result ) { } @ Override public int getValueCount ( ) { } @ Override public void get ( int docID , BytesRef result ) { } @ Override public int lookupTerm ( BytesRef key ) { } } public static class AssertingSortedSetDocValues extends SortedSetDocValues { private final SortedSetDocValues in ; private final int maxDoc ; private final long valueCount ; long lastOrd = NO_MORE_ORDS ; public AssertingSortedSetDocValues ( SortedSetDocValues in , int maxDoc ) { } @ Override public long nextOrd ( ) { } @ Override public void setDocument ( int docID ) { } @ Override public void lookupOrd ( long ord , BytesRef result ) { } @ Override public long getValueCount ( ) { } @ Override public long lookupTerm ( BytesRef key ) { } } @ Override public NumericDocValues getNumericDocValues ( String field ) throws IOException { } @ Override public BinaryDocValues getBinaryDocValues ( String field ) throws IOException { } @ Override public SortedDocValues getSortedDocValues ( String field ) throws IOException { } @ Override public SortedSetDocValues getSortedSetDocValues ( String field ) throws IOException { } @ Override public NumericDocValues getNormValues ( String field ) throws IOException { } @ Override public Object getCoreCacheKey ( ) { } @ Override public Object getCombinedCoreAndDeletesKey ( ) { } private final Object cacheKey = new Object ( ) ; }

Fix:
TermsEnum termsEnum = in . intersect ( automaton , bytes ) ;