K.P.
K.P.

Reputation: 67

Using parse4j, I am unable to cast ParseObject to custom class extending ParseObject

Using the following custom parse4j ParseObject classes:

@ParseClassName("Job")
public class Job extends ParseObject {

    public final String DOOR = "Door";

    public Job() {}

    public setDoor(Door door) {
        put(DOOR, door);
    }

    public Door getDoor() {
        return (Door) get(DOOR); // or (Door) getParseObject(DOOR);
    }
}

@ParseClassName("Door")
public class Door extends ParseObject {
    public Door() {}
}

and the following to load:

final static String TAG = "MainWindow";
ArrayList<Job> jobs = new ArrayList<Job>();

ParseQuery<Job> query = ParseQuery.getQuery(Job.class);
query.addAscendingOrder(Job.INDEX);
query.findInBackground(new FindCallback<Job>() {
    @Override
    public void done(List<Job> foundJobs, ParseException e) {
        if(e == null) {
            if(foundJobs == null) {
                System.out.println(TAG + " no jobs found");
            } else {
                for(final Job job : foundJobs) {
                    jobs.add(job);

                    try {
                        job.getParseObject(Job.DOOR).fetchIfNeeded();
                    } catch (ParseException e1) {
                        System.err.println(TAG + " e1: " + e1.getMessage());
                    }

                    System.out.println("objId: " + job.getDoor().getObjectId());
                }
            }
        } else {
            System.out.println(TAG + " e: " + e.getMessage());
        }
    }
});

I get the following error:

Exception in thread "pool-1-thread-5" java.lang.ClassCastException: org.parse4j.ParseObject cannot be cast to Door
    at Job.getDoor(Job.java:219)
    at MainWindow$8.done(MainWindow.java:327)
    at org.parse4j.ParseQuery$FindInBackgroundThread.run(ParseQuery.java:596)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

The error occurs at System.out.println("objId: " + job.getDoor().getObjectId());

Initially I thought it was because I left off the fetchInBackground() but it still wouldn't work. Any help is greatly appreciated.

The funny thing is I can create a new Job with the Door object linked just fine. Just retrieving it later is causing the problem.

I've even preloaded the list of Doors hoping that would solve the problem. I'm tempted to unlink the Door from the Job and just save the objectId String or custom UserID.

Upvotes: 3

Views: 94

Answers (0)

Related Questions