Set Iterator & NullPointerException
Greetings! I have an issue here that i can't find.
I am getting a NullPointerException at sets.put( nodes_iter.next(), null );
in the end of my DisjSet class code.
I just started making keySets of hashMaps and the like yesterday, so i think there might be something i don't know.
Thankful for every awnser-
Here are the code:
I create a new DisjSet with:
DisjSet<T> ds = new DisjSet<T>( theGraph.keySet() );
Here is theGraph i make a key set of:
private Map< T, HashSet<Edge> > theGraph = new HashMap< T, HashSet<Edge> >( );
Here is the relevant parts of the DisjSet class:
import java.util.*;
public class DisjSet<K extends Comparable<? super K>>
{
//HashMap containing 1. K itself, 2. Ks parent. K no.2 is null if K has no parent
private HashMap<K,K> sets;
public DisjSet(Set<K> s)
{
if(s.isEmpty())
开发者_开发知识库 throw new IllegalStateException("Empty DisjSet argument");
Iterator<K> nodes_iter = s.iterator();
while(nodes_iter.hasNext())
sets.put( nodes_iter.next(), null );
}
( ... )
}
You never initialize "sets", so you get an NullPointerException there.
BTW: This is an equal error to the one you had earlier this day. It's not a shame to make such errors, but you should try to learn from the answers.
Since nodes_iter.hasNext() check is there, that leaves the only possibility of sets being null.
精彩评论