我根据“Lucene in action”一书编写了简单的代码,它应该向我展示不同的分析器是如何工作的(使用 Lucene 4.3.0)。但是当我尝试增加 tokenStream 时,我会收到 NullPointerException。你有什么建议吗?下面我附上代码:
public static void main(String[] args) throws IOException {
System.out.println("\n----");
System.out.println("StandardAnalyzer");
displayTokensWithFullDetails(new StandardAnalyzer(Version.LUCENE_43),
"I'll email you at xyz@example.com");
}
public static void displayTokensWithFullDetails(Analyzer analyzer,
String text) throws IOException {
TokenStream stream = analyzer.tokenStream("contents",
new StringReader(text));
//defining attributes
while(stream.incrementToken()) {
//print details of tokens
}
System.out.println();
}
我无法开始循环。你知道我做错了什么吗?
在这里我添加堆栈跟踪:
----
StandardAnalyzer
Exception in thread "main" java.lang.NullPointerException
at org.apache.lucene.analysis.standard.StandardTokenizerImpl.zzRefill(StandardTokenizerImpl.java:923)
at org.apache.lucene.analysis.standard.StandardTokenizerImpl.getNextToken(StandardTokenizerImpl.java:1133)
at org.apache.lucene.analysis.standard.StandardTokenizer.incrementToken(StandardTokenizer.java:171)
at org.apache.lucene.analysis.standard.StandardFilter.incrementToken(StandardFilter.java:49)
at org.apache.lucene.analysis.core.LowerCaseFilter.incrementToken(LowerCaseFilter.java:54)
at org.apache.lucene.analysis.util.FilteringTokenFilter.incrementToken(FilteringTokenFilter.java:50)
at AnalyzerUtils.displayTokensWithFullDetails(AnalyzerUtils.java:115)
at AnalyzerUtils.main(AnalyzerUtils.java:169)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)