Java <METHOD_NAME> 的类型有误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22142145/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 13:52:21  来源:igfitidea点击:

The type of <METHOD_NAME> is erroneous

javanetbeans

提问by Oliver Watkins

I am getting a strange compile error in Netbeans.

我在 Netbeans 中遇到一个奇怪的编译错误。

I am creating an Experiment object and calling a run method on it.

我正在创建一个 Experiment 对象并在其上调用一个 run 方法。

    Experiment experiment=new Experiment();

    Result result = experiment.run(t, steps, trials, breadth, depth, seed, distribution);

The compiler complains that

编译器抱怨说

The type of run(Maplayout, int, int , int, int, long, int) is erroneous.

run(Maplayout, int, int , int, int, long, int) 的类型是错误的。

My method signature looks normal :

我的方法签名看起来很正常:

public Result run(MapLayout t, int steps, int trials, 
                        int breadth, int depth, long seed, int distribution)

I have double checked the paramaters I am passing in and they all seem normal. If I pass in :

我已经仔细检查了我传入的参数,它们看起来都很正常。如果我传入:

    experiment.run(null, 1,1,1,1,1l,1);

I get the same compile error on the run method.

我在 run 方法上遇到相同的编译错误。

Am i missing something obvious? Has too much Javascript damaged my brain?

我错过了一些明显的东西吗?太多的 Javascript 是否损坏了我的大脑?

回答by Supuhstar

I have the same problem on NetBeans 8.0. It seems that if you have this structure:

我在 NetBeans 8.0 上遇到了同样的问题。看来,如果你有这个结构:

ClassA:

ClassA

public interface ClassA {
}

ClassB:

ClassB

import ClassA;

public class ClassB implements ClassA {
}

ClassC:

ClassC

import ClassB;

public class ClassC extends ClassB {
}

ClassD:

ClassD

import ClassC;
import ClassA;

public class ClassD {
  public ClassA getClassA() {
    return new ClassC(); // error here
  }
}

you get this error:

你得到这个错误:

path\to\ClassC.java:7: error: The type of new ClassC() is erroneous
        return new ClassC();
               ^

because ClassCdoes not explicitly implement ClassA, the compiler doesn't know if the type is correct. I fixed it by making ClassCimplement ClassA, even though it already extends ClassB:

因为ClassC没有显式实现ClassA,编译器不知道类型是否正确。我通过制作ClassC工具修复了它ClassA,即使它已经扩展了ClassB

new ClassC:

ClassC

import ClassA;
import ClassB;

public class ClassC extends ClassB implements ClassA {
}

回答by mmavcy

You might need to import Result in the Experiment class.

您可能需要在 Experiment 类中导入 Result。

I just had the same problem and it was because of that.

我只是遇到了同样的问题,正因为如此。

回答by George Siggouroglou

I had the same issue and the solution was very simple in my case.

我遇到了同样的问题,就我而言,解决方案非常简单。

The case:
I copy/paste some classes from another project in a package of the project i am working in.
Some of them had the old package declaration and the compiler didn't complained (for his reasons).
When i used a method with return type one of the 'wrong packaged' classes this error appeared.
(The Type of is erroneous)

案例:
我将另一个项目中的一些类复制/粘贴到我正在工作的项目的一个包中。
其中一些具有旧的包声明并且编译器没有抱怨(出于他的原因)。
当我使用返回类型为“错误打包”类之一的方法时,出现此错误。
(类型错误)

The solution
To solvethe issue, I changed the package declaration to be the correct one!

解决方案
为了解决这个问题,我将包声明更改为正确的!

回答by cslysy

In my case (Netbeans 8.0.2) restart helps

在我的情况下(Netbeans 8.0.2)重启有帮助

回答by Jigar Shah

Just do a clean and build on the project again. Most of the time this is the error specific to Netbeans.

只需清理并再次构建项目即可。大多数情况下,这是 Netbeans 特有的错误。

回答by dellasavia

I faced this issue on Netbeans 7.4. Tried to reopen IDE, clean and rebuild but does not solve. In my case, there was an implementsclause on the erroneuos class. I removed this clause, declare it again and so the error has gone.

我在 Netbeans 7.4 上遇到了这个问题。尝试重新打开IDE,清理并重建但没有解决。在我的例子中,有一个implements关于 erroneuos 类的条款。我删除了这个子句,再次声明它,所以错误消失了。