1

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?

GhostCat
  • 137,827
  • 25
  • 176
  • 248
Acro
  • 21
  • 1
  • 8
  • 1
    Check your imports. – J-Alex Jul 12 '17 at 06:56
  • 1
    Are you using the right `Filter` class? E.g. is the import correct? Have you definitely recompiled `Filter` since adding those methods? – Andy Turner Jul 12 '17 at 06:56
  • Do you have two versions of class in your class path? – Rishikesh Darandale Jul 12 '17 at 06:58
  • Imports are working. The line "Filter filt = new Filter()" works, therefore the class is recognised – Acro Jul 12 '17 at 06:58
  • 2
    There are lots of classes called `Filter` - you might be getting the wrong one - show us your import statements. – greg-449 Jul 12 '17 at 07:06
  • And please reduce this to a [mcve]. There's a *lot* of code here that's unrelated to the problem, but you're missing the important parts: the imports and the package declaration for `Filter`, along with how you're trying to compile. We don't even know what this "other" compiler is... – Jon Skeet Jul 12 '17 at 07:16
  • Side note: you asked to review your code. Please understand that such requests are off-topic here. For working code, please turn to https://codereview.stackexchange.com/ - but read their policies carefully before posting content there. – GhostCat Jul 12 '17 at 08:00

2 Answers2

1

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

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • I guess the only reason that the initialization of the Filter works, is because another Filter class is found and used instead? And not the one I made? Or why else am I able to initialize the Filter but can't use it's methods? (Assuming if they are all correct, which was my main question) – Acro Jul 12 '17 at 07:44
1

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!

Acro
  • 21
  • 1
  • 8
  • Well. Glad you found an answer somehow. Feel free to practice upvoting now that you reached that level. – GhostCat Jul 12 '17 at 08:29