Issue
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
Solution
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.
Answered By - Grease