在计算java中的成本时出现NullPointerException

时间:2012-06-21 14:39:26

标签: java nullpointerexception classnotfoundexception

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

但是该方法在下一行给出了一个nullpointer异常。     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;

}

1 个答案:

答案 0 :(得分:1)

  1. portOnePathOutportTwoPathOut列表之一为空
  2. portList为空
  3. cloud.getBoundaryPriceMap()为空
  4. btp.getIntraPrice(rootBoundaryType)是数字包装器,它为null
  5. dataProducedFromP1dataProducedFromP2为空
  6. btp.getOutPrice(boundaryType)btp.getIntraPrice(rootBoundaryType)btp.getInPrice(boundaryType)为空
  7. 您调用的函数中的某个对象为null
  8. 下次发布堆栈跟踪时,列表会短得多。