Ben
Ben

Reputation: 62366

Why is my return value not properly recognized?

MyClass.java

protected LinkedBlockingDeque<JobSet> currentWork = new LinkedBlockingDeque<JobSet>();

public LinkedBlockingDeque<JobSet> getCurrentWork() {
    return currentWork;
}

Usage

public boolean completeAllWork(CompleteWorkRequest request) {
    for (JobSet jobSet : getCurrentWork()) {
        //if it's approved, find the workflow process it needs to go to next and put it there
        if (request.getApprovedJobSets().contains(jobSet.getUuid().toString())) {
            sendToNextWorkflowProcess(jobSet);
        } else {
            getCurrentWork().remove(jobSet);
            logger.info("Rejected JobSet: " + jobSet.getUuid());
        }
    }

    getWorkFromQueue();

    return true;
}

It's expecting a JobSet but getting an Object. It seems clear to me that it's returning the proper object, so what did I miss?

Error: java: incompatible types
  required: com.production.model.JobSet
  found:    java.lang.Object

Upvotes: 3

Views: 171

Answers (4)

James Dunn
James Dunn

Reputation: 8264

It could be that one of your methods is declared to return a LinkedBlockingDeque instead of a LinkedBlockingDeque<JobSet>.

  • Check the declared return types of getApprovedJobSets().
  • Look at the code in the method getWorkFromQueue(), perhaps the error is thrown there.

Also, does the compiler give any "unchecked type" or "raw type" warnings?

Upvotes: 0

Jeroen Vannevel
Jeroen Vannevel

Reputation: 44439

Per the comments: using an Iterator should solve the problem. My guess is there is interference while iterating the list and simultaneously removing an item, causing the loop to read a deleted value.

Upvotes: 2

Joni
Joni

Reputation: 111219

You cannot get the error that you mention from the code you have posted so far. This compiles without errors:

import java.util.concurrent.LinkedBlockingDeque;
import java.util.Collection;

class dummy {
    static class JobSet {public Object getUuid() {return null;}}
    static class CompleteWorkRequest {public Collection getApprovedJobSets() {return null;}};
    void getWorkFromQueue() {}
    void sendToNextWorkflowProcess(JobSet js) {}


    protected LinkedBlockingDeque<JobSet> currentWork = new LinkedBlockingDeque<JobSet>();

    public LinkedBlockingDeque<JobSet> getCurrentWork() {
        return currentWork;
    }

    public boolean completeAllWork(CompleteWorkRequest request) {
        for (JobSet jobSet : getCurrentWork()) {
            //if it's approved, find the workflow process it needs to go to next and put it there
            if (request.getApprovedJobSets().contains(jobSet.getUuid().toString())) {
                sendToNextWorkflowProcess(jobSet);
            } else {
                getCurrentWork().remove(jobSet);
                //logger.info("Rejected JobSet: " + jobSet.getUuid());
            }
        }

        getWorkFromQueue();

        return true;
    }
}

Upvotes: 0

miha1908
miha1908

Reputation: 177

Actually, if I understand correctly, your error does not come from the return statement, but from the two lines in the else statement. I think you should look at what that methods are returning and try to modify them

Upvotes: 0

Related Questions