以下方法计算成本。它在需要时访问其他类。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;
}