2011-02-23 56 views
0

我使用Lucene的API,我就在這行我的代碼以下錯誤:Java中,無法找到符號:方法方法名(org.bla.blabla.myClass)

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

錯誤: CollectionIndexer.java:34:找不到符號 symbol:方法add(org.apache.lucene.document.Field) location:class CollectionIndexer.Document _document.add(new Field(「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的-cp公地消化器-2.1 /公地消化器-2.1.jar:lucene的核 - 3.0.3的.jar myApp.java

+1

你叫什麼對象myMethod方法? – reef 2011-02-23 15:03:27

+1

這裏幾乎沒有足夠的信息來建議任何明智的!你可以添加一個minimable(非)可編譯的代碼片段嗎? – 2011-02-23 15:03:46

+0

@Patrick,請爲此問題提供[SSCCE](http://sscce.org/)。 – aioobe 2011-02-23 15:06:02

回答

1

問題出在你的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的內部類。您的Document類和Lucene的名稱之間有名稱衝突。當你用行Document _document = new Document(); instanciate你的文檔,你實際上instanciating你的文檔類。這就是編譯器無法找到add方法的原因。

多解決方案:

a。 Instanciate文檔前綴與Lucene包名稱

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

b。重命名你的內部類,以避免名稱衝突。

+0

@reef我得到這個錯誤:無法找到符號:方法getBytes()(和我導入java.io.Reader) – aneuryzm 2011-02-23 16:32:38

+0

您的getType()方法返回一個字符串?在我的例子中,我調用構造函數Field(String name,byte [] value,Field.Store store),所以你不需要導入java.io.Reader。在我的示例中調用的getBytes()方法來自String類(請參閱http://download.oracle.com/javase/1.5.0/docs/api/java/lang/String.html)。 – reef 2011-02-23 16:34:14

+0

@reef請忽略我的最後一條消息。我仍然得到相同的錯誤,現在我的代碼是:document.add(new Field(「type」,new byte [3],Field.Store.YES)); – aneuryzm 2011-02-23 16:39:34

0

基於更新的問題更新時間:

  • 確保解決此排隊和你的大括號沒有的東西否則會導致問題。
  • 將代碼減少到儘可能少的行,以消除任何可能通過編譯器關閉的其他項。
  • 如果可以,不用commons-digester-2.1進行編譯,以消除可能的衝突。
  • 將行對齊,以便在單獨的行上創建Field對象,而不是將字段添加到文檔中,以便確認構造函數調用沒有問題。
+0

我更新了我的問題與真正的實施 – aneuryzm 2011-02-23 15:30:25

3

當我難倒了這種類型的錯誤,通常是由於這樣的事實,我已經的InterfaceName兩個定義,一不小心進口錯了一個或多個地方。

(恰巧例如,當我不小心選擇java.awt.List代替java.util.List自動導入缺少的類時。)

確保...

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

...預期的包/類相匹配。

相關問題