0

enter image description hereI'm receiving the following error while casting

Caused by: java.util.concurrent.ExecutionException: java.lang.ClassCastException: org.json.JSONObject cannot be cast to NCRB_GUIs.DeviceGroupManager.FetcherForms.Serializable_JSONObject

My Serializable_JSONObject is defined as:

import java.io.Serializable;
import org.json.JSONObject;
public class Serializable_JSONObject extends JSONObject implements Serializable{}

I am unsure why I am getting this error. Here is the code where the exception is thrown...

    @Override
    public ReturnInterface<Serializable_JSONObject> call() throws Exception {
        CredentialInterface ci = (CredentialInterface) this.taskDeployInterface;
        JSONObject READ_NO_VALIDATION = WebGet.READ_NO_VALIDATION(ci.getUser(), ci.getPassword(), new URL(ci.getHost()));
        Serializable_JSONObject o = (Serializable_JSONObject) READ_NO_VALIDATION;//Error thrown here
        this.returnItem = o;
        return this;
    }
CoupFlu
  • 311
  • 4
  • 20

1 Answers1

1

I believe this issue is occurring as WebGet.READ_NO_VALIDATION(ci.getUser(), ci.getPassword(), new URL(ci.getHost())); line of code retruns a object of JsonObject not the Serializable_JSONObject and as Serializable_JSONObject is a sub class of JsonObject you can't cast JosnObject's object to sub class object. You can debug that READ_NO_VALIDATION is object of which class by READ_NO_VALIDATION.getClass().

Amit Bera
  • 7,075
  • 1
  • 19
  • 42
  • If you look at the definition of Serializable_JSONObject, you will see it is a simple extension of JSONObject which implements serializable. It should be able to be cast. – CoupFlu Jun 06 '18 at 17:20
  • I have updated the code as above. The error is thrown at the cast. – CoupFlu Jun 06 '18 at 17:22
  • To cast an object to Serializable_JSONObject class object, it must need to be the object of Serialiezable_JSONObject or it's subsclass object. You can't cast superclass object to subclass. – Amit Bera Jun 06 '18 at 17:23
  • Can you please just print the class of that object using : `System.out.println(READ_NO_VALIDATION.getClass());` – Amit Bera Jun 06 '18 at 17:26
  • You are right. What would be the easiest way to make this conversion? – CoupFlu Jun 06 '18 at 17:28
  • Class= class org.json.JSONObject [ProgressBarHelper]done! Jun 06, 2018 1:29:37 PM org.openide.util.Exceptions printStackTrace SEVERE: null java.util.concurrent.ExecutionException: java.util.concurrent.ExecutionException: java.lang.ClassCastException: org.json.JSONObject cannot be cast to NCRB_GUIs.DeviceGroupManager.FetcherForms.Serializable_JSONObject at java.util.concurrent.FutureTask.report(FutureTask.java:122) – CoupFlu Jun 06 '18 at 17:30
  • For that, I need more information and the context for that. Need to look at the definition of `WebGet#READ_NO_VALIDATION()` – Amit Bera Jun 06 '18 at 17:30
  • This is based off apache's org.json. return XML.toJSONObject(sb.toString()); – CoupFlu Jun 06 '18 at 17:34
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/172618/discussion-between-amit-bera-and-jamie-snipes). – Amit Bera Jun 06 '18 at 17:36
  • @JamieSnipes Not exactly. Please follow the link it may help you:https://stackoverflow.com/questions/20534862/how-to-efficiently-map-a-org-json-jsonobject-to-a-pojo – Amit Bera Jun 06 '18 at 18:30