When i add more than one Fieldable to my Document i get a nullpointer exception(without any exception description) when i try to add the document to the indexwriter.
i only changed the fieldable method called stringvalue, to return a string.
Is it not allowed to add more fieldables, or am i missing something ?
code that might be relevant
File[] files = FILES_TO_INDEX_DIRECTORY.listFiles();
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_33);
SimpleFSDirectory d = new SimpleFSDirectory(INDEX_DIRECTORY);
IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_33, analyzer);
IndexWriter indexWriter = new IndexWriter(d, iwc);
for (File file : files) {
try
{
final String path = file.getCanonicalPath();
final String name = file.getName();
Fieldable field1 = new Fieldable()
{ removed }
Fieldable field2 = new Fieldable() {also removed}
Document document = new Document();
document.add(field1);
document.add(field2);
List<Fieldable> minliste = document.getFields();
indexWriter.addDocument(document); //this is where it fails
}
Fieldable :
Fieldable field1 = new Fieldable()
{
@Override
public int getBinaryLength() {
return 0;
}
@Override
public int getBinaryOffset() {
return 0;
}
@Override
public byte[] getBinaryValue() {
return null;
}
@Override
public byte[] getBinaryValue(byte[] arg0) {
return null;
}
@Override
public float getBoost() {
return 0;
}
@Override
public boolean getOmitNorms() {
return false;
}
@Override
public boolean getOmitTermFreqAndPositions() {
return false;
}
@Override
public boolean isBinary() {
return false;
}
@Override
public boolean isIndexed() {
return false;
}
@Override
public boolean isLazy() {
return false;
}
@Override
public boolean isStoreOffsetWithTermVector() {
return false;
}
@Override
public boolean isStorePositionWithTermVector() {
return false;
}
@Override
public boolean isStored() {
return false;
}
@Override
public boolean isTermVectorStored() {
return false;
}
@Override
public boolean isTokenized() {
return false;
}
@Override
public String name() {
//TODO
return "path";
}
@Override
public Reader readerValue() {
return null;
}
@Override
public void setBoost(float arg0) {
}
@Override
public void setOmitNorms(boolean arg0) {
}
@Override
public void setOmitTermFreqAndPositions(boolean arg0) {
}
@Override
public String stringValue() {
//TODO
return path;
}
@Override
public TokenStream tokenStreamValue() {
return null;
}
};
You’re implementing your
Fieldablesfrom scratch. I suspect that the problem is hidden somewhere in your removed lines – maybe one of the implemented methods returnnullwhere it should return a real value.Fieldinstead and check if you still have the same errorNullPointerException, that should help finding the real cause of your problem.Now that you’ve added the implementation of your
FieldableI’m pretty sure that the NPE occurs because you return somenullvalues in your implementation.A hot candidate is your implementation of
readerValue()which returnsnull. JavaDoc explains, that theReaderthat is returned by this method can be used at index time to generate index tokens. An indexing is what happens automatically when you add more then oneFieldableto a document.Give this a try:
Change your implementation and avoid return
nullunless the JavaDoc clearly says, thatnullis a legal return value.(and: even if
Fieldis deprecated: use it for a test to see, if the problem is related to yourFieldables. If your code works withFieldobjects, then you know where to look)