开发者

multiple fieldables in document causes nullpointerexception

开发者 https://www.devze.com 2023-03-17 18:29 出处:网络
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.

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 f开发者_如何学Goalse;
            }

            @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 Fieldables from scratch. I suspect that the problem is hidden somewhere in your removed lines - maybe one of the implemented methods return null where it should return a real value.

  1. Try to add instances of Field instead and check if you still have the same error
  2. Use a debugger an add a breakpoint on NullPointerException, that should help finding the real cause of your problem.

Now that you've added the implementation of your Fieldable I'm pretty sure that the NPE occurs because you return some null values in your implementation.

A hot candidate is your implementation of readerValue() which returns null. JavaDoc explains, that the Reader that 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 one Fieldable to a document.

Give this a try:

@Override
public Reader readerValue() {
     return new StringReader(path);
}

Change your implementation and avoid return null unless the JavaDoc clearly says, that null is a legal return value.

(and: even if Field is deprecated: use it for a test to see, if the problem is related to your Fieldables. If your code works with Field objects, then you know where to look)

0

精彩评论

暂无评论...
验证码 换一张
取 消