-->

Eclipse is telling me a method is undefined when i

2019-02-16 02:27发布

问题:

I'm working in RAD 7.5. I'm importing a package from one web project into another. I instantiate the class and try to use one of its method, but I get a build error that the method is undefined. The method is public. All other methods work except this one. The project from which I'm importing is built properly. I've tried deleting both projects, grabbing them from version control again, and rebuilding them. What on earth could possible be the problem or what else can I check in eclipse (RAD 7.5)?

Additional information:

The method being called is not static. It is public and being called from a not static, protected, method.

Code snippets

Class I'm importing:

package com.state;

public TelcoVariableTracker() {
    super();
}

public boolean isMedicalFlag() {
    return isMedicalFlag;
}

Class with the build error:

import com.state.TelcoVariableTracker;

protected method() {

TelcoVariableTracker phoneInfo = HttpSessionUtils.getTelcoVariableTracker(request);

    if (phoneInfo.isMedicalFlag() {  // Build error: The method isMedicalFlag() is undefined for the type TelcoVariableTracker
        // Do things
    }
}

回答1:

The class can be declared twice in the same package. Therefore only the first class is loaded, and the second class has your missing method.

What a mess, you hide a method from yourself :D



回答2:

I had same issue, Cleaning the project did the trick :)



回答3:

This Eclipse plugin Classpath Helper can show blocked classes: http://classpathhelper.sourceforge.net/

Blocked classes happen when you have identical classes in different jar.



回答4:

I had this same issue. Eclipse was picking up the same code from same two jars, even though they were declared in two separate workspaces. I was using projects - workspace and projects - workspace_new.

I removed the other workspace and did a clean project, but to no avail at first. I had multiple locations where the identical jars were. I actually had to shut my laptop down to clear out everything as closing out and restarting eclipse wouldn't fix it. After I did that however, everything resolved and the compile errors went away. Then I added the workspace back in I had removed and eclipse was able to keep things clean.



回答5:

You have a duplicate class in your classpath. In my case, Schema.getLogicalType() gave a method undefined error but the other Schema methods were working fine and would show up in Eclipse assist. I highlighted getLogicalType and pressed F3 and linked the source. Sure enough the method existed. I found the fix when I opened the .classpath. The source was linked to a different jar file. It got linked to phoenix-4.7.0-HBase-1.1-client.jar and it had an older org.apache.avro.Schema package inside it. The phoenix.jar conflicted with my avro-1.8.1.jar. So I just removed the phoenix.jar from the classpath to fix the problem. Hope this gives you an idea on how to fix a method undefined error.



回答6:

I also had this issue. In my case I had written the call to the function before actually creating the function so initially the error was expected. However after the function was created the error remained. I seems eclipse cached the error in the tab and didn't recheck the error when switching between tabs. Closing and reopening the tab fixed it for me.



回答7:

It could be that you have the class in your source code and the class of the same name, but older and without that method, in the jar. And you see the jar class.



回答8:

I had the same issue. Closing and opening the tab was not enough. Had to restart eclipse.