0

我不明白列表元素如何在我的程序中如此莫名其妙地受到 null 的影响。在需求列表上执行调用层次结构,这是我得到的:

1:

 private void checkCompletion(int bidId){
        Bid bid = BidOperation.removeBid(getRunningAgrts(), bidId);
        BidOperation.addBidToList(getExecutedRes(), bid);

        if (BidOperation.difference(getRequirements(), 
                getExecutedRes()).size() == 0){
            finishExecution();
            SimTable.incrementSucessRate();
        }   
    }

2:

public boolean checkFirstSetComplete(){ 
    if (BidOperation.difference(getRequirements(), 
            getTentAndFinaPartialRes()).size() == 0)
        return true;
    else return false;
}

3:

boolean equality2 = BidOperation.equality(bid.getResourceList(), 
            getRequirements());

4:

protected double expectedCost(){
    double sum = 0;

    for (ResChar elt : getRequirements()){
        sum += elt.getNbCopies() * elt.getUnitCost() * usageTime();
    }
    return sum;
}

5:

public Bid generateOffer(Integer sellerId, List<ResChar> sellerSpec){
...
if ((getFullFinalAgrts().size() + getFullTentAgrts().size() == 0) &&
            (BidOperation.difference(getRequirements(), sellerSpec).size() == 0))
...
}

6:

private boolean intersectSellerBuyer(List<ResChar> sellerRes){

    for (ResChar sellerResource : sellerRes){

        for (ResChar buyerResource : getRequirements()){

            if (sellerResource.getTypeId() == buyerResource.getTypeId()){
                return true;
            }
        }
    }
    return false;
}

7:

private void mainLoop(){
    int t = (int)CloudSim.clock();
    for ( Map.Entry<Integer, List<ResChar>> entry : sellerCharacteristicsList.entrySet()){
        if (intersectSellerBuyer(entry.getValue()) && 
                !negociateWithSeller(entry.getKey())){
            sendNow(entry.getKey(), CSBTs.BUYER_OFFER, 
                    generateOffer (entry.getKey(),entry.getValue()));
        }
    }
    if ((estpB != -1) && (t > estpB)){
        decommitAndOrCancelAllPartialBids(t);
    }
    if (t > tBK){
        k--; tBK = (bTimes.getDlB() - t) / k + t;

        if (BidOperation.difference(getRequirements(), 
                getTentAndFinaPartialRes()).size() > 0){
            decommitAndOrCancelAllPartialBids(t);
        }
    }
}

8:

private double partDenominator(){
    double sum = 0;
    for (ResChar elt : getRequirements()){
        sum += elt.getNbCopies() * unitCost(elt) * BidOperation.ratio(elt);
    }
    return sum;
}

9:

private boolean runningTask(){
    boolean runningTask = false;
    if (isRunningTask())
        return true;
    else if (BidOperation.difference(getRequirements(), getRunningRes()).size() == 0){
        System.out.println("The running resource :" +getRunningRes().size());
        System.out.println("The requirements :" +getRequirements().size());
        setRunningTask(true);
        runningTask = true;
    }       
    return runningTask;
}

最后是 BidOperation.diferrence 方法:

public static List<ResChar> difference(List<ResChar> list1, List<ResChar> list2){
    ResourceList list = new ResourceList(list1);
    Iterator<ResChar> listItr = list.resourceList.listIterator();
    Iterator<ResChar> listItr2 = list2.listIterator();
    int diff;
    while (listItr.hasNext()){
        ResChar elt = listItr.next();
        while (listItr2.hasNext()){ 
            ResChar elt2 = listItr2.next();
            if (elt2.getTypeId() == elt.getTypeId()){
                diff = elt.getNbCopies() - elt2.getNbCopies();
                if (diff > 0)
                    elt.setNbCopies(diff);  
                else
                    listItr.remove();
                break;      
            }   
        }   
    }   

    return list.resourceList;
}

使用最后一种方法是为了不直接修改需求列表。出于这个原因,我使用了复制构造函数。谁能有足够的时间来解释我在运行时将 null 分配给需求元素的如此混乱的分配?

谢谢你。

4

0 回答 0