java map help

edited February 2008 in Science & Tech
ok, so in my code in the constructor, i'm taking in a set and map and using them to create another map. the new map is going to contain all the number of times all the different types of Strings in the old map appear. (like object {A, D, C, B, E} with key 5 meaning that A, D, C, B, E appears 5 times in the old map. heres my code(*note; the main isnt here):

[PHP]/*Election*/
import java.util.*;
public class Election
{ private Map<Integer, String> totals = new HashMap<Integer, String>();
public Election(Set<String> s, Map<Integer, String> m)
{ Set<String> stemp = s;
Map<Integer, String> mtemp = m;
int num = 1; String temp = "";
do
{ temp = mtemp.get(1);
ArrayList<Integer> del = new ArrayList<Integer>();
for(int poop : mtemp.keySet())
{ if(temp.equals(mtemp.get(poop)))
{ num++;
del.add(poop);
}
}
for(int poop = 0; poop < del.size(); poop++)
mtemp.remove(poop);
totals.put(num, temp);
}while(!mtemp.isEmpty());
System.out.println("kkkk"+totals.toString());
}
}[/PHP]

ok, the problem that i ran into is at [PHP]if(temp.equals(mtemp.get(poop)))[/PHP] i'm getting a nullPointerException. i dont get y this is happening. can someone help me?

the map m is:
{1=AbBaCdDc, 2=CdBaDcAb, 3=BaDcCdAb, 4=CdBaAbDc, 5=CdBaAbDc, 6=CdBaDcAb, 7=AbBaCdDc, 8=CdBaAbDc, 9=AbBaCdDc, 10=AbBaCdDc, 11=CdBaDcAb, 12=AbBaCdDc}

and the set s is:
[Ab, Dc, Cd, Ba]

Comments

  • JBJB Carlsbad, CA
    edited February 2008
    Try stepping through with your debugger. The data set is small enough that you should find the problem in a few minutes.

    On a side note, I would recommend much much more descriptive names (and comments) so it is easier for someone else to read your code.
  • shwaipshwaip bluffin' with my muffin
    edited February 2008
    do 
            {    temp = mtemp.get(1);
                ArrayList<Integer> del = new ArrayList<Integer>(); 
                for(int poop : mtemp.keySet()) 
                {    if(temp.equals(mtemp.get(poop))) 
                    {    num++; 
                        del.add(poop); 
                    } 
                } 
                for(int poop = 0; poop < del.size(); poop++) 
                    mtemp.remove(poop); 
                totals.put(num, temp); 
            }while(!mtemp.isEmpty()); 
    
    <code style="white-space: nowrap;"><code></code></code>what happens in the time through the while loop where there is only one item in mtemp?<code style="white-space: nowrap;"><code></code></code>
Sign In or Register to comment.