nullpointerexception - Specific java method return null pointer exception -


i want specify whether specific node neighbor set of nodes in defined graph or not? purpose wrote method:

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;     } 

unfortunately method return null pointer exception. decided change to:

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;     } 

the new method works fine takes huge resource , time , useless. question how can handle null pointer exception in way defined method takes less time execution?

i debugged method. here information 3 used objects:

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 

as can see none of specified objects null! caused npe?!

as see, have 2 different instances of same object in different collections. because of search name, these lines:

for(customer customer: collection){     for(customer cust:nodes){         if(cust.getname().equals(customer.getname())) acctualnodes.add(cust);     } } 

and use issuccessor method correct instances

if assumtion right way go is:

private boolean isneighbor(arraylist<customer> collection, customer node,directedsparsegraph<customer, transaction> network) throws sqlexception {         hashmap<string, customer> realnodes = new hashmap<string, customer>();         collection<customer> nodes=network.getvertices();          (customer n: nodes) {             realnodes.put(n.getname(), n);         }          customer acctualnode = realnodes.get(node.getname());         for(customer customer:collection){             customer actualcustomer = realnodes.get(customer.getname());             if(network.issuccessor(actualcustomer, acctualnode)) {                 return true;             }         }         return false;     } 

edit - added notes: work relatively fast, belive bottle neck in network object, getting node doesn't me. if there method returns customernode name need use instad of putting objects in hashmap.

edit2 - try make faster. don't know package use jung implementation of directsparsegraph if so. found in sources of jung implementation of getsuccessor method:

public collection<v>  getsuccessors(v vertex)     {         if (!containsvertex(vertex))             return null;         return collections.unmodifiablecollection(getsuccs_internal(vertex));     } 

and there no issuccessor. made assumption issuccessor method throws npe because getsuccessors method return null. returns null because passed vertex cannot found. vertexes in collection comparing using equals method. here purpousal: define equals method in customer object , compare name (better define equals , hashcode methods , generate them ide - eclipse, idea, netbeans):

public class customer { ...  private string name;      @override     public int hashcode() {         final int prime = 31;         int result = 1;         result = prime * result + ((name == null) ? 0 : name.hashcode());         return result;     }      @override     public boolean equals(object obj) {         if (this == obj)             return true;         if (obj == null)             return false;         if (getclass() != obj.getclass())             return false;         customer other = (customer ) obj;         if (name == null) {             if (other.name != null)                 return false;         } else if (!name.equals(other.name))             return false;         return true;     } } 

after can try use first approach. if doesn't work assumption issuccessor method , npe isn't right , try define issuccessor method self:

private boolean isneighbor(arraylist<customer> collection, customer node,directedsparsegraph<customer, transaction> network) throws sqlexception {         for(customer customer:collection){             //if(network.issuccessor(customer, node)) {              if(issuccessor(network, customer, node)) {                 return true;             }         }         return false;     }      private boolean issuccessor(directedsparsegraph<customer, transaction> network, customer customer, customer node) {         customer mocknode = node;         customer mockcustomer = customer;         // if can't redefine equals method customer object need create mock object mocknode.equals(node) == true && mockcustomer.equals(customer)         collection<customer> successors = network.getsuccessors(mocknode);         return successors != null && successors.indexof(mockcustomer) != -1;     } 

if can't redefine equals method customer object need create mock objects before pass them network object, following condition should true:

mocknode.equals(node) && mockcustomer.equals(customer) 

but better if can define equals , hascode methods believe significantly.


Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -