정의 된 그래프에서 특정 노드가 노드 집합과 이웃인지 여부를 지정 하시겠습니까? 이 목적을 위해이 메소드를 작성했습니다 :특정 Java 메소드 반환 null 포인터 예외
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
지정된 객체 중 null이 없습니다. 그래서 NPE가 원인이 될 수 있습니다!
왜 null 포인터 예외가 발생합니까? null이되는 것은 무엇입니까? – engma
첫 번째 접근법이 더 좋은 것으로 보이고 NPE에 대한 세부 사항을 설명하지 않았습니다. 'network' 필드가'null'입니까? 'isSuccessor' 메소드는 어떻게 생겼을까요? 누군가가'null' 컬렉션이나'null' 엔트리가있는 컬렉션을 전달 했습니까? (또한, 당신이 방법을 쓰고있을 때 당신은 받아 들일 수있는 것에 융통성을 가져야한다. 어떤 목록을 가져올 수도 있고, Collection도 괜찮을 것이다.) –
chrylis
먼저 NPE를 얻을 때 예외 추적을 통해 정확한 위치를 알려줍니다. 어쩌면 우리에게 말해야 할 것입니다. 둘째, 실제로 어떤 포인터가 null인지 파악하기 위해 디버그해야합니다. –