以下方法计算成本。它在需要时访问其他类。 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;
}
答案 0 :(得分:1)
portOnePathOut
和portTwoPathOut
列表之一为空portList
为空cloud.getBoundaryPriceMap()
为空btp.getIntraPrice(rootBoundaryType)
是数字包装器,它为null dataProducedFromP1
或dataProducedFromP2
为空btp.getOutPrice(boundaryType)
,btp.getIntraPrice(rootBoundaryType)
或btp.getInPrice(boundaryType)
为空下次发布堆栈跟踪时,列表会短得多。