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

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/lucene/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.

Related

The constructor TFSTeamProjectCollection(String) is undefined

Trying to implement the Java SDK, following the tutorial here: https://blogs.msdn.microsoft.com/bharry/2011/05/16/announcing-a-java-sdk-for-tfs/
I have the libraries imported, but getting a compilation error
TFSTeamProjectCollection tpc =
new TFSTeamProjectCollection(BASEURL);
BASEURL is a string I defined earlier in the code.
The error is: The constructor TFSTeamProjectCollection(String) is undefined
Does anyone know how to resolve this issue?
Well, studying the C# side of things ( see here ) I can't find a constructor that would only take a String argument.
There is only a single argument constructor taking a Uri.
In other words: the fact that you your string contains a Url; and that you named it a BASEURL doesn't magically turn it from a String into a URL or URI class object.
Guessing: the ctor wants an argument of type java.net.URL which you could probably create with something like new URL(BASEURL); instead of just passing BASEURL to that constructor.
The thing is: in order to actually understand which constructors that Team Foundation class has; one would need access to the corresponding SDK from Microsoft - which you probably downloaded and and have in place. So, the only thing that you need to do ... read the javadocs!

I am getting Cannot find symbol error while using dependencies.getEdgeSet()

I am working with Collapsed dependencies using Stanford CoreNLP.
I am getting
cannot find Symbol. Symbol:method getEdgeSet()
error while typing the following code:
Set<SemanticGraphEdge> edge_set1 = dependencies.getEdgeSet();
No other errors are found. I have already imported
edu.stanford.nlp.semgraph.SemanticGraphEdge;
Why does it happen so?
The type of dependencies is SemanticGraph which doesn't have the method getEdgeSet().
In the first paragraph in the documentation you can see:
There is no mechanism for returning all edges at once (eg edgeSet()). This is intentional. Use edgeIterable() to iterate over the edges if necessary.
See getAllEdges(IndexedWord gov, IndexedWord dep) and edgeIterable() instead.

Constructor error while creating an empty dataset in weka

I am trying to classify an instance using the classifyInstance method (described in weka's documentation here) using the Matlab environment.
This method require the instance to be link to a dataset. I am trying to use this constructor to create an empty dataset with the following matlab code:
import java.util.ArrayList.*;
import weka.core.*;
import weka.core.Instances.*;
attInfo = java.util.ArrayList;
attInfo.add(weka.core.Attribute('att1'));
attInfo.add(weka.core.Attribute('att2'));
attInfo.add(weka.core.Attribute('att3'));
dataset= weka.core.Instances(java.lang.String('relation'), attInfo, 2);
When I try to run this code matlab return me the following error:
No constructor 'weka.core.Instances' with matching signature found.
Error in file_name (line 109) dataset =
weka.core.Instances(java.lang.String('relation'), attInfo, 5);
What is wrong with the parameters of my constructor?
I end up finding the solution of the problem. The constructor accept a signature which use the deprecated class FastVector. I just added a snapshot of my code in case it might help someone.
attInfo = FastVector();
attInfo.addElement(weka.core.Attribute('att1'));
attInfo.addElement(weka.core.Attribute('att2'));
attInfo.addElement(weka.core.Attribute('att3'));
% build the class attribute
classValues = FastVector();
classValues.addElement(java.lang.String('0'));
classValues.addElement(java.lang.String('1'));
attInfo.addElement(Attribute('Class', classValues));
% create the dataset and define the class attribute
dataset = Instances('relation', attInfo, 1);
dataset.setClassIndex(dataset.numAttributes() -1);
% build the instance
Inst = weka.core.Instance(10);
for ii = 1:D.numAttributes()
Inst.setValue(D.attribute(ii-1), 1)
end
Inst.setDataset(dataset)
% classify the instance
classifier.classifyInstance(Inst)
The use of java object such as java.lang.String() also lead to an error.
I am still curious about why this is happening, but I suspect that might be because of the version of weka that I am using (3.6.11) where the documentation might be for the version 3.7.12 .

TestRig in ANTLRworks: how to use own classes?

I'm trying to build a MT940 parser using antlr4. The grammar is simple but works for most cases.
Now I want to return my own classes. This works:
file returns [String myString]
:
Header940? record+ EOF
;
I think this is becasue String is in the default java packages.
I want this:
file returns [List<MT940Record> records]
:
Header940? record+ EOF
;
The TestRig complains (logically):
/tmp/TestRigTask-1392235543340/MT940_5aParser.java:50: error: cannot find symbol
public List<MT940Record> records;
^
symbol: class MT940Record
location: class FileContext
How can I set the CLASSPATH / lib directory in the TestRig in ANLTRWorks?
In ANTLRWorks, you can't. You can add an issue for this on the issue tracker:
https://github.com/sharwell/antlrworks2/issues
Note that ANTLR 4 was designed so you no longer need to use user-defined arguments and/or return values in your grammar. Instead of returning a List<MT940Record> like you described above, you should use a listener or visitor after the parse is complete to compute the necessary result.

Parsing java code and adding methods with AST (JDT Eclipse)

I have a .java file which contains a class. I want to add a method to that class but I can't find a real useful "HOWTO" or examples around.
I'm using Eclipse and its JDT plugin for AST.
I tried a code that creates an ICompilationUnit from a project
IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject("ProjName");
IJavaProject javaProject = JavaCore.create(project);
IPackageFragment package1 = javaProject.getPackageFragments()[0];
ICompilationUnit unit = package1.getCompilationUnits()[0];
then add a method with astrewrite.
But it seems to work only if I run all as a Plugin Project and not a simple Java Application.
I need to write an application in java that "simply" parse a java file and adds method to its class.
What I supposed to do is:
1) Create an ICompilationUnit directly form the .java file I want to parse (eventually located in my own project's directory)
2) Using another way
Both case I can't go further. Anyone can help me?
When you need to make a change by adding something to the compilation unit, you will have to use the functions provided by CompilationUnit to create new nodes.
To add a method to "unit" you will have to :
Create a MethodDeclaration node using your compilation unit :
MethodDeclaration md = unit.getAST().newMethodDeclaration();
Customize this method declaration to your requirements :
md.setName( unit.getAST().newSimpleName( "newMethod" ) );
md.setBody( unit.getAST().newBlock() );
this will produce : void newMethod() {}
Obtain the TypeBinding from "unit" :
TypeDeclaration typeDeclaration = ( TypeDeclaration )unit.types().get( 0 );
Add your newly created MethodDeclaration to the body declarations :
typeDeclaration.bodyDeclarations().add( md );
There's a method called getMethods() on TypeDeclaration but it doesn't return a live list of MethodDeclarations, therefore you can't modify that directly.
It's really easy to read the source file as text and replace the last } with the method declaration plus }. Obviously this doesn't work if someone puts multiple top-level classes in one file (which is extremely rare and I doubt you'll have a problem with that).

Categories