Java <class> 的类型是 netbeans 中的错误错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23459741/
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 23:08:00  来源:igfitidea点击:

The Type of <class> is erroneous error in netbeans

javanetbeans

提问by Paul

I have defined two classes for a java program I am writing, call them Class1 and Class2. In the body of the constructor for Class1, I call the constructor for class 2. However, I am getting the compile error

我为我正在编写的 Java 程序定义了两个类,将它们称为 Class1 和 Class2。在 Class1 的构造函数的主体中,我调用了类 2 的构造函数。但是,我收到了编译错误

 "The type of Class1(JSONObject) is erroneous". 

I tried googling this error but could not find any discussion of this exact error anywhere, so I thought I would post it to stack exchange.

我尝试在谷歌上搜索这个错误,但在任何地方都找不到关于这个确切错误的任何讨论,所以我想我会把它发布到堆栈交换中。

Could someone please explain what type of error this is? Both class1 and class2 are very simple: both have only a constructor method, which takes a JSONObject as an argument in both cases. The only imports are for JSON. Any advice?

有人可以解释一下这是什么类型的错误吗?class1 和 class2 都非常简单:它们都只有一个构造函数方法,在两种情况下都将 JSONObject 作为参数。唯一的导入用于 JSON。有什么建议吗?

//class1 definition
public class Class1 {
       public Class1(JSONObject jObject){
           try{
           //parsing json and saving class variables
           } catch(Exception e)
           {
               System.out.println("Class1 JSON Exception: " + e.getMessage());
           }
       }
}


//constructor of Class2
Class1 user;

public Class2(JSONObject jObject){
    try{
    JSONObject userJSON = jObject.getJSONObject("user");
    user = new Class1(userJSON); //error occurrs here
    }
    catch(Exception e){
   System.out.println("Class2 JSON Exception: " + e.getMessage());

    }
}

}

EDIT: when I try to run the code even with this compile error, I get the following runtime error:

编辑:当我尝试运行代码时,即使出现此编译错误,我也会收到以下运行时错误:

Exception in thread "main" java.lang.ExceptionInInitializerError
at bitcoin.thesis.Client.main(BTCJamClient.java:18)
Caused by: java.lang.RuntimeException: Uncompilable source code - Erroneous tree type:        
thesis.JSONArray
at thesis.Class3.<clinit>(Class3.java)
... 1 more
Java Result: 1

Class3 here is another class that has a default constructor. Client is the main class which takes the http request and passes the JSON object to Class2 constructor. Basically this is the part of the code execution before Class1 and Class2 constructors are even called. So it is not caused directly by the compile error, but I suspect they are related to the same problem that has do do more generally with my coding environment.

这里的 Class3 是另一个具有默认构造函数的类。Client 是主类,它接受 http 请求并将 JSON 对象传递给 Class2 构造函数。基本上这是在 Class1 和 Class2 构造函数被调用之前的代码执行部分。所以它不是由编译错误直接引起的,但我怀疑它们与我的编码环境更普遍的相同问题有关。

Thanks, Paul

谢谢,保罗

采纳答案by Grease

I would have preferred to leave this as a comment but as I do not have the reputation I couldn't. I realise this is also a very late response but don't know if you have found the answer or not. I came across this while googling for an answer myself.

我更愿意将此作为评论,但由于我没有我不能的声誉。我意识到这也是一个很晚的回复,但不知道您是否找到了答案。我自己在谷歌上搜索答案时遇到了这个问题。

I also believe that this error is unrelated to the code but is rather an error created by NetBeans. I found the same code compiled and ran fine in NetBeans on one machine but not the on the other where I had first encountered the error.

我也相信这个错误与代码无关,而是由 NetBeans 创建的错误。我发现相同的代码在一台机器上的 NetBeans 中编译并运行良好,但在我第一次遇到错误的另一台机器上却没有。

The solution for me was to close NetBeans, clear the NetBeans cache and restart NetBeans. I was using version 8.0 and the location of the cache for me is:

我的解决方案是关闭 NetBeans,清除 NetBeans 缓存并重新启动 NetBeans。我使用的是 8.0 版,我的缓存位置是:

~/.cache/netbeans/8.0/

~/.cache/netbeans/8.0/

I deleted everything in the folder and on the next run everything was fine.

我删除了文件夹中的所有内容,在下次运行时一切正常。

For older versions I believe the cache may be in a different location which can be found by opening the about window from the help menu.

对于旧版本,我相信缓存可能位于不同的位置,可以通过从帮助菜单中打开关于窗口来找到该位置。

回答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 prashanth-g

I faced this issue. I solved it by adding all dependent jars to the project/file

我遇到了这个问题。我通过将所有依赖的 jars 添加到项目/文件来解决它

回答by bonzi

I had the same issue on netbeans 8.0. The following trick should fix it:

我在netbeans 8.0上遇到了同样的问题。以下技巧应该可以解决它:

Right click on the project -> properties -> build -> compiling ==> uncheck "compile on save" then click ok

右键单击项目 -> 属性 -> 构建 -> 编译 ==> 取消选中“保存时编译”,然后单击确定

回答by Lanil Marasinghe

Make sure that you have entered correct package names in your classes.

确保您在类中输入了正确的包名称。

回答by json

Grease's answer re deleting the contents of netbeans cache works. I'm using Netbeans 8.1 on MacOS Sierra.

Grease 的回答重新删除了 netbeans 缓存的内容。我在 MacOS Sierra 上使用 Netbeans 8.1。

Deleted everything under "/Users//Library/Caches/NetBeans/8.1/"

删除了“/Users//Library/Caches/NetBeans/8.1/”下的所有内容

回答by NoNaMe

I got the same issue and fixed using the excepted answer, as I'm using windows 10 so here is the cache folder location:

我遇到了同样的问题并使用例外答案进行了修复,因为我使用的是 Windows 10,所以这里是缓存文件夹位置:

C:\Users\USER_NAME\AppData\Local\NetBeans\Cache\8.1

C:\Users\USER_NAME\AppData\Local\NetBeans\Cache\8.1

AppData is a hidden folder.

AppData 是一个隐藏文件夹。