Lucene 4.3.0的简单测试代码不起作用

时间:2013-05-26 10:24:49

标签: java apache testing lucene

我根据书中的“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();
  }

我无法启动循环。你知道我做错了吗?

这里我添加了stacktrace:

----
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)

1 个答案:

答案 0 :(得分:2)

确定。我找到了解决方案启动TokenStream后,StandardTokenizationImpl不接收Reader对象但接收null。当我们在流StandardTokenizationImpl上输入reset()时,最后我们会收到Stream。所以我的代码应如下所示:

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));

    stream.reset(); //Add this line removes NullPointerException

    //defining attributes

    while(stream.incrementToken()) {

      //print details of tokens

    }
    System.out.println();
  }