日期:2014-05-19  浏览次数:20874 次

分词器IKSegmentation报错
package com.haitian.IKAnalyerTest.Analyer;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import org.wltea.analyzer.IKSegmentation;

public class SecondAnalyer {

public static void main(String[] args) {
Reader input;
try {
input = new FileReader("E:\\test.txt");
IKSegmentation ikSegmentation = new IKSegmentation(input);
while (ikSegmentation.next() != null) {
System.out.println(ikSegmentation.next().getLexemeText());
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

错误如下:

Exception in thread "main" java.lang.NullPointerException
at com.haitian.IKAnalyerTest.Analyer.SecondAnalyer.main(SecondAnalyer.java:17)


------解决方案--------------------
IKSegmentation ikSegmentation = new IKSegmentation(input);
Lexeme lexeme; 
while ((lexeme = ikSegmentation.next()) != null) {
System.out.println(lexeme.getLexemeText());
}

ikSegmentation.next()运行一次就会发生迭代往下查,你一个println就发生了两次next
如果你分词数是双数就会NullPointerException