-2

以下方法计算成本。它在需要时访问其他类。Cloud 类保存定价信息。

但是,该方法在下一行给了我一个空指针异常。BoundaryPrice btp = cloud.getBoundaryPriceMap().get(boundaryType); totalcost 的值永远不会改变。
有什么想法可以突出这里的问题吗?

private float getCostForConnector(Connector connector)
{
    Cloud cloud = new Cloud("http://amazon.com/europeEC2Clouds#dublinEC2Cloud", dynDesInventory);
    List<Port> portList = getListOfPort(connector);
    Resource root = findCommonRoot(connector);

    Port p1 = portList.get(0);
    Port p2 = portList.get(1);
    float totalCost = 0.0f;
    try
    {
    String rootBoundaryType = getRootBoundaryType();

    Float dataProducedFromP1 = getDataCountForPort(p1);
    Float dataProducedFromP2 = getDataCountForPort(p2);

    List<String> portOnePathOut =  getListOfNetworkBoundary(p1, root);
    List<String> portTwoPathOut =  getListOfNetworkBoundary(p2, root);

        for(String boundaryType: portOnePathOut)
        {
            BoundaryPrice btp = cloud.getBoundaryPriceMap().get(boundaryType);
            if (btp != null)
            {
                totalCost += btp.getOutPrice(boundaryType) + dataProducedFromP1;
                totalCost += btp.getInPrice(boundaryType) + dataProducedFromP2;
            }
        }

        for(String boundaryType: portTwoPathOut)
        {
            BoundaryPrice btp = cloud.getBoundaryPriceMap().get(boundaryType);
            if (btp != null)
            {
                totalCost += btp.getOutPrice(boundaryType) + dataProducedFromP2;
                totalCost += btp.getInPrice(boundaryType) + dataProducedFromP1;
            }
        }

        BoundaryPrice btp = cloud.getBoundaryPriceMap().get(rootBoundaryType);
        if (btp != null)
        {
            totalCost += (dataProducedFromP1 + dataProducedFromP2) * btp.getIntraPrice(rootBoundaryType);
        }


    }catch(NullPointerException e) {
        System.err.println("Caught NullPointerException: ");
    }
    return totalCost;

}
4

1 回答 1

1
  1. portOnePathOutportTwoPathOut 列表之一为空
  2. portList一片空白
  3. cloud.getBoundaryPriceMap()一片空白
  4. btp.getIntraPrice(rootBoundaryType)是数字包装器,它为空
  5. dataProducedFromP1dataProducedFromP2为空
  6. btp.getOutPrice(boundaryType),btp.getIntraPrice(rootBoundaryType)btp.getInPrice(boundaryType)为空
  7. 您调用的函数内部的某些对象为空

下次发布堆栈跟踪时,该列表会短得多。

于 2012-06-21T14:49:58.573 回答