- package com.util.analyseWords;
-
- import java.io.IOException;
- import java.io.Reader;
- import java.io.StringReader;
- import java.util.ArrayList;
- import java.util.List;
-
- import org.wltea.analyzer.core.IKSegmenter;
- import org.wltea.analyzer.core.Lexeme;
-
- public class AnalyseWords {
- private List<String> words = new ArrayList<String>();
- public AnalyseWords(String query){
- Reader reader = new StringReader(query);
- IKSegmenter ik = new IKSegmenter(reader, true);
- try {
- Lexeme lex = null;
- while(true){
- lex = ik.next();
- words.add(lex.getLexemeText());
- }
- } catch (NullPointerException e) {
- //System.out.println("--null--");
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
- public List<String> getWords() {
- return words;
- }
-
- public void setWords(List<String> words) {
- this.words = words;
- }
- public static void main(String[] args) {
- AnalyseWords an = new AnalyseWords("hadoop action");
- for(String word : an.getWords()){
- System.out.println(word);
- }
- }
- }
复制代码
错误提示空指针异常:- Exception in thread "main" java.lang.ExceptionInInitializerError
- at org.wltea.analyzer.core.IKSegmenter.<init>(IKSegmenter.java:63)
- at com.util.analyseWords.AnalyseWords.<init>(AnalyseWords.java:16)
- at com.util.analyseWords.AnalyseWords.main(AnalyseWords.java:38)
- Caused by: java.lang.NullPointerException
- at org.wltea.analyzer.cfg.DefualtConfig.<init>(DefualtConfig.java:79)
- at org.wltea.analyzer.cfg.DefualtConfig.<clinit>(DefualtConfig.java:43)
- ... 3 more
复制代码
|