最近在学习Lucene时遇到了问题,先上代码:
public class AnalyzerUtil { public void displayToken(String str, Analyzer a) { try { TokenStream stream = a.tokenStream("content", new StringReader(str)); // PositionIncrementAttribute pia = stream.addAttribute(PositionIncrementAttribute.class); CharTermAttribute cta = stream.addAttribute(CharTermAttribute.class); // TypeAttribute ta = stream.addAttribute(TypeAttribute.class); while(stream.incrementToken()) { System.out.println(cta); } System.out.println("---------------------------------------------"); } catch (IOException e) { e.printStackTrace(); } } }
public class AnalyzerTest { private AnalyzerUtil au = null; @Before public void before() { au = new AnalyzerUtil(); } @Test public void AnalyzerTest() { String str = "this is my book,thank you"; StandardAnalyzer a1 = new StandardAnalyzer(Version.LUCENE_42); au.displayToken(str, a1); } }
运行结果:
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:180) 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 com.my.test.analyzer.AnalyzerUtil.displayToken(AnalyzerUtil.java:23) at com.my.test.analyzer.AnalyzerTest.AnalyzerTest(AnalyzerTest.java:24) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at org.junit.internal.runners.TestMethodRunner.executeMethodBody(TestMethodRunner.java:99) at org.junit.internal.runners.TestMethodRunner.runUnprotected(TestMethodRunner.java:81) at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34) at org.junit.internal.runners.TestMethodRunner.runMethod(TestMethodRunner.java:75) at org.junit.internal.runners.TestMethodRunner.run(TestMethodRunner.java:45) at org.junit.internal.runners.TestClassMethodsRunner.invokeTestMethod(TestClassMethodsRunner.java:75) at org.junit.internal.runners.TestClassMethodsRunner.run(TestClassMethodsRunner.java:36) at org.junit.internal.runners.TestClassRunner$1.runUnprotected(TestClassRunner.java:42) at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34) at org.junit.internal.runners.TestClassRunner.run(TestClassRunner.java:52) at org.junit.runner.JUnitCore.run(JUnitCore.java:130) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
解决办法:
在进行incrementToken之前进行reset。问题随之解决。
运行结果: