为什么我的返回值未被正确识别?

时间:2013-07-26 14:24:19

标签: java

MyClass.java

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;
}

期待JobSet,但获得Object。我似乎很清楚它正在返回正确的物体,所以我错过了什么?

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

4 个答案:

答案 0 :(得分:2)

根据评论:使用Iterator应解决问题。我的猜测是在迭代列表并同时删除一个项目时会产生干扰,导致循环读取已删除的值。

答案 1 :(得分:0)

可能是您的某个方法被声明为返回LinkedBlockingDeque而不是LinkedBlockingDeque<JobSet>

  • 检查getApprovedJobSets()
  • 的声明返回类型
  • 查看方法getWorkFromQueue()中的代码,可能会抛出错误。

此外,编译器是否提供任何“未检查类型”或“原始类型”警告?

答案 2 :(得分:0)

实际上,如果我理解正确,你的错误不是来自return语句,而是来自else语句中的两行。 我认为你应该看一下这些方法的回归并尝试修改它们

答案 3 :(得分:0)

您无法从目前为止发布的代码中获得错误。这编译没有错误:

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;
    }
}