简体   繁体   English

Lucene 4.1中的简单代码抛出NullPointerException

[英]Simple code in Lucene 4.1 throwing a NullPointerException

Tokenizer tokenizer 
           = new org.apache.lucene.analysis.standard.StandardTokenizer
                                    (Version.LUCENE_41,new StringReader("!!!"));
tokenizer.incrementToken();

Throws 抛出

java.lang.NullPointerException
    at org.apache.lucene.analysis.standard.std34.StandardTokenizerImpl34.zzRefill(StandardTokenizerImpl34.java:812)
    at org.apache.lucene.analysis.standard.std34.StandardTokenizerImpl34.getNextToken(StandardTokenizerImpl34.java:1019)
    at org.apache.lucene.analysis.standard.StandardTokenizer.incrementToken(StandardTokenizer.java:180)

This code was working okay with Lucene 36, why is it thorweing an exception 这段代码与Lucene 36一起正常工作,为什么它会出现异常

I had the exact same problem as you (ie NullPointerException after upgrading to 4.1 from 3.6), although I am using a org.apache.lucene.analysis.TokenStream . 我遇到了与你完全相同的问题(即从3.6升级到4.1后的NullPointerException),尽管我使用的是org.apache.lucene.analysis.TokenStream After digging around in the Lucene source, I found that calling the reset method before calling incrementToken addresses the issue. 在Lucene源代码中挖掘后,我发现在调用incrementToken之前调用reset方法解决了这个问题。

You can have a look at the documentation for the previous version here: 您可以在此处查看以前版本的文档:

https://lucene.apache.org/core/old_versioned_docs/versions/3_5_0/api/all/org/apache/lucene/analysis/standard/StandardTokenizer.html#reset(java.io.Reader) https://lucene.apache.org/core/old_versioned_docs/versions/3_5_0/api/all/org/apache/lucene/analysis/standard/StandardTokenizer.html#reset(java.io.Reader)

Perhaps you replaced the text passed to the StringReader in your production code when you posted it here, but "!!!" 也许您在此处发布时替换了生产代码中传递给StringReader的文本,但是"!!!" seems like something that could lead to odd behavior to me. 似乎有些东西可能导致我的奇怪行为。 Since the string is entirely punctuation, I don't believe there are any tokens there for it to consume. 由于字符串完全是标点符号,我不相信它有任何令牌供它使用。 I'm honestly not sure what .incrementToken() should do when called with an input that has no tokens in it. 我真的不确定.incrementToken()在使用没有令牌的输入调用时应该做什么。

Have you tried it using an input with valid tokens? 您是否使用带有效令牌的输入进行了尝试?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM