首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2EE开发 >

分词器IKSegmentation报错解决方案

2012-05-22 
分词器IKSegmentation报错package com.haitian.IKAnalyerTest.Analyerimport java.io.FileNotFoundExcept

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

热点排行