有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

lucene Java,找不到符号:方法methodName(org.bla.blabla.myClass)

我正在使用Lucene API,在这行代码中出现以下错误:

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()));

错误: 收藏索引。java:34:找不到符号 符号:方法添加(org.apache.lucene.document.Field) 位置:class CollectionIndex。文件 _文件。添加(新字段(“type”,document.getType())

这是有关该方法的文档: http://lucene.apache.org/java/3_0_3/api/all/org/apache/lucene/document/Document.html#add(org.apache.lucene.document.Fieldable

谢谢

更新:javac-cpcommons-digester-2.1/commons-digester-2.1。jar:lucene-core-3.0.3。jarmyapp。爪哇


共 (3) 个答案

  1. # 1 楼答案

    当我被这类错误难倒时,通常是因为我有两个InterfaceName的定义,并且意外地在一个或多个地方导入了错误的定义

    (例如,在自动导入缺少的类时,我意外地选择了java.awt.List而不是java.util.List。)

    确保

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

    。。。与预期的包/类匹配

  2. # 2 楼答案

    根据问题的更新进行更新:

    • 确保你的花括号围绕着这条线,并且没有其他东西引起问题
    • 将代码减少到尽可能少的行,以消除可能通过编译器关闭的任何其他项
    • 如果可以,编译时不使用commons-digester-2.1,以消除可能的冲突
    • 将行拆分,以便在单独的行上创建字段对象,而不是将字段添加到文档中,以便确认构造函数调用没有问题
  3. # 3 楼答案

    问题来自这样一个事实:document.getType()方法返回一个字符串,然后 在Field类中没有与调用匹配的构造函数。 见http://lucene.apache.org/java/3_0_3/api/all/org/apache/lucene/document/Field.html

    如果我在我的环境中测试您的代码,Eclipse会说:

    The constructor Field(String, String) is undefined

    也许你可以这样做:

    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);
    

    提交源代码后更新

    问题来自这样一个事实:在类中有一个名为Document的内部类。文档类与Lucene的类之间存在名称冲突。当您使用Document _document = new Document();行实例化文档时,实际上是在实例化文档类。这就是编译器找不到add方法的原因

    多种解决方案:

    a.实例化以Lucene包名称作为前缀的文档

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

    重命名内部类,这样就不会有任何名称冲突