15
votes

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

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

Thanks, Paul

9
Post your current code. - Luiggi Mendoza
May be you have changed something and saved but classes is not generated. configure Compile On Save - Braj
1) Post the exact error message (copy/paste the actual text, 2) Post the code with the line that gave the error. 3) Post any related code that preceded that line. - FoggyDay
Thanks everyone. I was thinking that this kind of error might be something unrelated to the code, like what Braj suggested. But I will post the relevant code. - Paul
What is userJSON and where did you define it? - Vimal Bera

9 Answers

49
votes

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.

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:

~/.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.

4
votes

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

2
votes

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 solve the issue, I changed the package declaration to be the correct one!

2
votes

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

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

AppData is a hidden folder.

1
votes

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

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

0
votes

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

0
votes

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

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

0
votes

This is an addition to the answer of Grease. (I can't add a comment due to lack of reputation.)

I sometimes run into the same issue and always deleted the complete folder, so NetBeans had to rebuild the maven-stuff which can take a few minutes.

If someone uses maven and don't want NetBeans has to completely rebuild the maven repos and -index, don't delete the following folders in the NetBeans cache folder:

  • index
  • mavencachedirs
  • mavenindex

Just tested it with NetBeans 11.3: Not deleting the three folders makes the error(s) go away but don't bothers maven. I could imagine that this is the same with older versions of NetBeans.

0
votes

I decided to stop/restart NetBeans 8.2 before taking the "nuclear" approach. The error was gone when I got back in.