-1

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 );
}
( ... )
}

2 Answers2

1

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.

Community
  • 1
  • 1
Boris
  • 4,327
  • 2
  • 19
  • 27
  • Yes, thank you. I know... i felt quite ashamed :p. Have been staring at my code all day so i'm getting a bit unfocused :/ – Alexander E Feb 27 '11 at 19:33
1

Since nodes_iter.hasNext() check is there, that leaves the only possibility of sets being null.

Murali VP
  • 6,198
  • 4
  • 28
  • 36