我想指定特定节点是否与定义图中的节点集相邻?为此,我编写了这个方法:
private boolean isNeighbor(ArrayList<Customer> collection, Customer node,DirectedSparseGraph<Customer, Transaction> network) throws SQLException {
for(Customer customer:collection){
if(network.isSuccessor(customer, node)) return true;
}
return false;
}
不幸的是,这个方法返回空指针异常。所以我决定将其更改为:
private boolean isNeighbor(ArrayList<Customer> collection, Customer node,DirectedSparseGraph<Customer, Transaction> network) throws SQLException {
Collection<Customer> nodes=network.getVertices();
ArrayList<Customer> acctualNodes = new ArrayList<Customer>();
Customer acctualNode=new Customer();
for(Customer customer: collection){
for(Customer cust:nodes){
if(cust.getName().equals(customer.getName())) acctualNodes.add(cust);
}
}
for(Customer customer: nodes){
if(node.getName().equals(customer.getName())) acctualNode=customer;
}
for(Customer customer: acctualNodes){
if(network.isSuccessor(customer, acctualNode)) return true;
}
return false;
}
新方法运行良好,但需要大量资源和时间,而且毫无用处。我的问题是如何以定义的方法执行时间更短的方式处理空指针异常?
我调试了我的方法。以下是有关三个使用对象的信息:
collection: ArrayList<E> id=17
elementData Object[6246] (id=37)
node: Customer id=23
customerArray null
customerName "9379090484" (id=1345)
type null
network: DirectedSparseGraph<V,E> id=27
edge_type EdgeType (id=39)
edges HashMap<K,V> (id=42)
vertices HashMap<K,V> (id=47)
entrySet HashMap$EntrySet (id=1349)
hashSeed -949367244
keySet HashMap$KeySet (id=48)
loadFactor 0.75
modCount 64780
size 64780
table HashMap$Entry<K,V>[131072] (id=52)
threshold 98304
useAltHashing false
values null
如您所见,没有一个指定的对象是空的!那么是什么导致了NPE?!