特定のノードが、定義されたグラフ内の一連のノードと隣接しているかどうかを指定したいですか? この目的のために、私はこの方法を書きました:
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;
}
残念ながら、このメソッドは null ポインター例外を返します。だから私はそれを次のように変更することにしました:
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;
}
新しい方法は問題なく機能しますが、膨大なリソースと時間がかかり、役に立ちません。私の質問は、定義されたメソッドの実行時間が短縮されるように、null ポインター例外を処理するにはどうすればよいですか?
メソッドをデバッグしました。使用した 3 つのオブジェクトに関する情報は次のとおりです。
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
ご覧のとおり、指定されたオブジェクトはどれも null ではありません! では、何が NPE を引き起こしたのでしょうか?!