2013-03-19 126 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循环