Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot cast to an Extended Class in Java

I looked around for a similar issue but couldn't find anything that matched it.

I'm trying to extend the built-in JSONObject to add some functionality, like so:

public class MyJSONObject extends JSONObject {

    // Easily return an integer from a JSONObject, handling when the value is null.
    //
    public Integer getIntegerUnlessNull(String key) throws JSONException {
        String key_value = this.getString (key);

        if ( key_value.equals("null") ) {
            return null;

        } else {
            return Integer.parseInt( key_value );
        }
    }
}

However, when I try to cast it, I get a java.lang.ClassCastException error:

private JSONArray    jsonClients;
        MyJSONObject clientJSONRecord;

clientJSONRecord = (MyJSONObject) jsonClients.getJSONObject(0);

The full error message is:

java.lang.ClassCastException: org.json.JSONObject cannot be cast to com.insightemissions.trak.extensions.MyJSONObject

Any help?

Cheers,

JP

like image 642
Joshua Pinter Avatar asked Dec 06 '13 04:12

Joshua Pinter


People also ask

Can you cast to a subclass?

As it was said before, you can't cast from superclass to subclass unless your object was instantiated from the subclass in the first place.

Can you cast object to subclass Java?

In java object typecasting one object reference can be type cast into another object reference. The cast can be to its own class type or to one of its subclass or superclass types or interfaces.

How do I fix Java Lang ClassCastException?

To prevent the ClassCastException exception, one should be careful when casting objects to a specific class or interface and ensure that the target type is a child of the source type, and that the actual object is an instance of that type.

What causes a class cast exception?

Introduction. ClassCastException is a runtime exception raised in Java when we try to improperly cast a class from one type to another. It's thrown to indicate that the code has attempted to cast an object to a related class, but of which it is not an instance.


2 Answers

jsonClients.getJSONObject(0) returns an object of the type JSONObject which is your parent type.

You cannot cast it to the inherited type. It only works the other way, i.e. casting an inherited class to a parent class. This has nothing to do with your objects in particular, it just the way inheritance works.

Because you get an instance of JSONObject from the method and you cannot control how it's instantiated, you could add a constructor to your MyJSONObject class to create an object from the parent object:

public MyJSONObject(JSONObject parent) {
    super(parent.toString());
}

And use it this way:

JSONObject parent = jsonClients.getJSONObject(0);
MyJSONObject child = new MyJSONObject(parent);
like image 112
Szymon Avatar answered Oct 07 '22 19:10

Szymon


The problem you have is that the objects inside the JSONArray (I presume the JSONArray object is created by the library) do not contain MyJSONObject objects that are defined by you.

Your code would work only if you created the JSONArray yourself and populated it with MyJSONObject objects.

Given what you are trying to achieve with this "extended functionality", I think inheritance is much of an overkill.

Why not just use a helper method?

public Integer getIntegerUnlessNull(JSONObject, String key) throws JSONException {
    String key_value = object.getString (key);

    if ( key_value.equals("null") ) {
        return null;

    } else {
        return Integer.parseInt( key_value );
    }
}

Then you can just do this:

Integer getInteger = getIntegerUnlessNull(object, "key");
if (getInteger == null) {
    // if null do something
}
like image 29
Lai Xin Chu Avatar answered Oct 07 '22 18:10

Lai Xin Chu