2013-03-19 133 views
0

我使用lucene從wiki轉儲進行查詢並獲取類別。所以,我得到相關文件和每個文件,我打電話給下面的函數。lucene維基百科查詢

static List<String> getCategories(Document document) throws IOException 
{ 
    List<String> categories = new ArrayList<String>(); 
    String text = document.get("text"); 
    WikipediaTokenizer tf = new WikipediaTokenizer(new StringReader(text)); 

    CharTermAttribute termAtt = tf.addAttribute(CharTermAttribute.class); 
    TypeAttribute typeAtt = tf.addAttribute(TypeAttribute.class); 

    while (tf.incrementToken()) 
    { 
     String tokText = termAtt.toString(); 
     if (typeAtt.type().equals(WikipediaTokenizer.CATEGORY) == true) 
     { 
      categories.add(tokText); 
     } 
    } 

    return categories; 
} 

但它在while語句中引發以下錯誤。

Exception in thread "main" java.lang.NullPointerException 
    at org.apache.lucene.analysis.wikipedia.WikipediaTokenizerImpl.zzRefill(WikipediaTokenizerImpl.java:574) 
    at org.apache.lucene.analysis.wikipedia.WikipediaTokenizerImpl.getNextToken(WikipediaTokenizerImpl.java:781) 
    at org.apache.lucene.analysis.wikipedia.WikipediaTokenizer.incrementToken(WikipediaTokenizer.java:200) 
    at SearchIndex.getCategories(SearchIndex.java:82) 
    at SearchIndex.main(SearchIndex.java:54) 

我看着zzRefill()函數,但它不能理解它。這是一個已知的錯誤或什麼?我不知道我做錯了什麼。 lucene傢伙說,整個wikipediaTokenizer部分處於測試階段,可能會有所變化。我希望有人能幫助我。

+0

當一個對象變量解除引用(如在'SearchIndex.getCategories')時,會發生'NullPointer'異常之前解決了這個問題,但對象變量沒有按實際上包含一個對象(沒有調用「new」)。它看起來像是在'while'循環條件下的'tf.IncrementToken()中發生的。 – 2013-03-19 19:19:51

+0

但我在哪裏投入新的?我真的很抱歉,但我幾乎沒有編程在Java中...我正在寫一個快速的黑客程序,以完成某項工作... – shashydhar 2013-03-19 19:22:27

+0

是'getCategories'你的代碼?它發生在Lucene中,所以沒有源代碼,我不知道如何排除故障。 – 2013-03-19 19:22:58

回答

1

我通過添加tf.reset()調用while循環