读书人

分词器IKSegmentation报错解决方案

发布时间: 2012-05-22 18:18:54 作者: rapoo

分词器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

读书人网 >J2EE开发

热点推荐