开发者

Java, cannot find symbol : method methodName(org.bla.blabla.myClass)

开发者 https://www.devze.com 2023-02-13 03:18 出处:网络
I\'m using Lucene APIs, and I get the following error on this line of my code: import org.apache.lucene.document.Document;

I'm using Lucene APIs, and I get the following error on this line of my code:

import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Fieldable;

...

Document _document  = new Document();
_document.add(new Field("type", document.getType()));

Error: CollectionIndexer.java:34: cannot find symbol symbol : method add(org.apache.lucene.document.Field) location: class CollectionIndexer.Document _document.add(new Field("type", document.getType()));

This is the documentation about the method: http://lucene.apache.org/java/3_0_3/api/all/org/apache/l开发者_如何学编程ucene/document/Document.html#add(org.apache.lucene.document.Fieldable)

thanks

Update: javac -cp commons-digester-2.1/commons-digester-2.1.jar:lucene-core-3.0.3.jar myApp.java


When I'm stumped over this type of error, it is usually due to the fact that I've two definitions of InterfaceName, and accidentally imported the wrong one in one or more places.

(Happens for instance when I accidentally choose java.awt.List instead of java.util.List when auto-importing missing classes.)

Make sure that ...

symbol  : method methodName(org.bla.blabla.myClass)
                            \____________________/
                               ... this part ...

... matches the expected package / class.


The problem comes from the fact that your document.getType() method returns a String and there is no constructor in the Field class that matches your call. See http://lucene.apache.org/java/3_0_3/api/all/org/apache/lucene/document/Field.html.

If I test your code in my environment Eclipse says:

The constructor Field(String, String) is undefined

Maybe you could do as the following:

Document _document = new Document();
_document.add(new Field("type", document.getType().getBytes(), Store.YES);
// Or document.add(new Field("type", document.getType().getBytes(), Store.NO);

UPDATE after source code submission --------------------

The problem comes from the fact that in your class you have an inner-class called Document. There is a name conflict between your Document class and the Lucene's one. When you instanciate your document with the line Document _document = new Document(); you're actually instanciating YOUR Document class. That's why the compiler cannot find the add method.

Multiple solution:

a. Instanciate the Document prefixing it with the Lucene package name

org.apache.lucene.document.Document _document = new org.apache.lucene.document.Document();

b. Rename your inner class so that you don't have any name conflict.


Updated based on updates to question:

  • Make sure your curly braces around this line up and that there is not something else causing an issue.
  • Reduce the code down just to as few lines as possible to eliminate any other items that could be throughing the compiler off.
  • Compile without the commons-digester-2.1 if you can, to eliminate possible conflicts.
  • Break the line up so the a Field object is created on a separate line than adding the field to the document so that you can confirm there is no problem with your constructor call.
0

精彩评论

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