[英]NullPointerException in Lucene TF-IDF Score calculator
I have made a TF-IDF score calculator in Lucene 6.1.0 . 我在Lucene 6.1.0中做了一个TF-IDF分数计算器。 Even passing the field name and term name ,My score calculator is showing Null Pointer exception . 即使传递字段名称和术语名称,我的分数计算器也会显示Null Pointer异常。 Below is the part of code where in main class where error is there. 以下是在主类中存在错误的代码部分。
public static void main(String[] args) throws IOException {
Tf_Idf tfidf = new Tf_Idf();
String field = "contentfield";
String term = "Reuters";
tfidf.scoreCalculator(field, term); //Line 144
}
Now scoreCalculator function is as follows: 现在,scoreCalculator函数如下:
public void scoreCalculator (String field, String term) throws IOException
{
TFIDFSimilarity tfidfSIM = new ClassicSimilarity();
// Bits liveDocs = MultiFields.getLiveDocs(this.indexReader);
//line 247 TermsEnum termEnum = MultiFields.getTerms(this.indexReader, field).iterator();
BytesRef bytesRef=null;
while ((bytesRef = termEnum.next()) != null) {
if(bytesRef.utf8ToString().trim().equals(term.trim())) {
if(termEnum.seekExact(bytesRef)) {
int doc;
idf = tfidfSIM.idf(termEnum.docFreq(), this.indexReader.numDocs());
PostingsEnum docsEnum = termEnum.postings(null);
if(docsEnum != null) {
doc=0;}
while((doc = docsEnum.nextDoc()) != DocIdSetIterator.NO_MORE_DOCS) {
tf = tfidfSIM.tf(docsEnum.freq());
tfidf_score = tf * idf ;
System.out.println(" -tfidf_score-" + tfidf_score);
}
}
}
}
}
}
The error Information is 错误信息是
Exception in thread "main" java.lang.NullPointerException
at Lucene.Tf_Idf.scoreCalculator(IndexFiles.java:247)
at Lucene.IndexFiles.main(IndexFiles.java:144)
The indexing is done but score calculator is not working.Also I think I am wrong at passing values to term and field.Please help me to to figure out what to pass if not this. 索引已完成,但分数计算器不起作用。此外,我认为将值传递给术语和字段是错误的。如果不是这样,请帮助我找出要传递的内容。
EDIT: 编辑:
Yes I have opened the index reader in tf- idf constructor. 是的,我已经在tf-idf构造函数中打开了索引读取器。
class Tf_Idf {
static float tf = 1;
static float idf = 0;
private float tfidf_score;
static float [] tfidf = null;
IndexReader indexReader;
public Tf_Idf() throws IOException {
String indexPath = "/home/kriti/index4";
this.indexReader = DirectoryReader.open(FSDirectory.open(Paths.get(indexPath)));
}
Also I found that after changing //String Field="contentsfield"
String Field="contents"
; 我也发现更改//String Field="contentsfield"
String Field="contents"
; Now no errors are shown but still it is not calculating the tf-idf values.Am i passing wrong string or something else is there?Besides these are some warning messages I am getting Alongside 现在没有错误显示,但仍然没有计算tf-idf值。我是否传递了错误的字符串或其他内容?此外,还有一些警告信息
Null point access:Variable docsEnum can only be null at this location
The value of local variable doc is not used
These are the names of field I have given: 这些是我给的字段名称:
static void indexDoc(IndexWriter writer, Path file, long lastModified) throws IOException {
try (InputStream stream = Files.newInputStream(file)) {
// make a new, empty document
Document doc = new Document();
Field pathField = new StringField("path", file.toString(), Field.Store.YES);
doc.add(pathField);
Field modifiedfield=new LongPoint("modified", lastModified);
doc.add(modifiedfield);
Field contentfield=new TextField("contents", new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8)));
doc.add(contentfield);
if (writer.getConfig().getOpenMode() == OpenMode.CREATE) {
System.out.println("adding " + file);
writer.addDocument(doc);
} else {
System.out.println("updating " + file);
writer.updateDocument(new Term("path", file.toString()), doc);
}
}
}
}
Looks like this.indexReader
is null. 看起来像this.indexReader
为null。 Make sure you've opened the reader (ie. in your Tf_Idf
constructor) before you call the scoreCalculator method. 在调用scoreCalculator方法之前,请确保已打开阅读器(即,在Tf_Idf
构造函数中)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.