Acro
Acro

Reputation: 21

Java can not find method of class

I am trying to call a method of my selfmade "Filter" class, but the Compiler can not find the method.

It is a little bit tricky, because I write my code in Eclipse where everything seems to be fine. But I have to copy my code into another Software and compile it there as well. This Compiler on the other hand can not find my method.

I assume that the Eclipse Compiler maybe "allows" more errors of which I am not even aware of. The other Compiler however struggles with them.

Here is what the "other" Compiler prints as error code:

C:\Program Files\Enomic\enomic-server\data\rules\testcompileboehmch\CodeTest.java:77: error: cannot find symbol filt.setHasRemoved(true);
symbol:   method setHasRemoved(boolean)
location: variable filt of type tms.Filter

I have no idea why my class is incorrect. As said above in eclipse everything works fine.

The CodeTest Class(cut down to the important part):

package tms;

import tms.Filter;
import java.util.*;

    Filter filt = new Filter();
    filt.setHasRemoved(true);//cannot be found

My Filter class:

package tms;

import java.util.ArrayList;
import java.util.List;

public class Filter {

private List<Object> remainingList;
private List<Object> removedList;
private Object typ;
private boolean hasRemoved;

public Filter()
{
    this.remainingList = new ArrayList<Object>();
    this.removedList = new ArrayList<Object>();
    this.typ = new Object();
    this.hasRemoved = false;
}   
public Filter(List<Object> remaining, List<Object> removed, Object typ, boolean hasRemoved)
{
    this.remainingList = new ArrayList<>();
    if(remaining != null)
    {
        this.remainingList.addAll(remaining);
    }
    this.removedList = new ArrayList<>();
    if(removed != null)
    {
        this.removedList.addAll(removed);
    }
    this.typ = typ;
    this.hasRemoved = hasRemoved;
}

//Set-Methoden      
public void setRemainingList(List<Object> list)
{
    this.remainingList.clear();
    this.remainingList.addAll(list);
}
public void setRemovedList(List<Object> list)
{
    this.removedList.clear();
    this.removedList.addAll(list);
}
public void setTyp(Object val)
{
    this.typ = val;
}
public void setHasRemoved(boolean val)
{
    this.hasRemoved = val;
}

//Get-Methoden
public List<Object> getRemainingList()
{
    return this.remainingList;
}
public List<Object> getRemovedList()
{
    return this.removedList;
}
public Object getTyp()
{
    return this.typ;
}
public boolean getHasRemoved()
{
    return this.hasRemoved;
}
} 

I really do not know why this doesn't work. Is there any mistake I can't see?

Upvotes: 1

Views: 5005

Answers (2)

Acro
Acro

Reputation: 21

By renaming my Filter Class I was able to solve the problem. It looks like the Compiler never knew exactly to which class i was referring. Nevertheless thanks for all the help, you directed me into the right direction.

Note to myself (and maybe others too): Come up with your own class names!

Upvotes: 1

GhostCat
GhostCat

Reputation: 140437

Here:

C:\Program Files\Enomic\enomic-server\data\rules\testcompileboehmch\CodeTest.java:77: error: cannot find

And

package tms;

The point is: the java compiler expects that the folder structure resembles the package structure.

So your problem is that your classes do not exist in a directory named tms.

It is rather strange that eclipse works here either. In that sense: you want to read here or there for example.

And no, there are only very few subtle things where the eclipse java compiler works differently than other products. Rest assured: chances that you as newbie run into such things are very close to zero. Your problems are caused by the fact that you do not understand the basics of how java files are compiled. ( that is the downside when you start learning programming using an IDE such as eclipse - the IDE hides many of these things from you. and then, when you need it - you have no idea what is going on).

Upvotes: 1

Related Questions