Hashmap counter giving NPE error
This code:
publicstaticvoid compareHand(String[] hand){
String[] tokens =new String[hand.length];
Map rank =new HashMap();
Map suit =new HashMap();
for (int j = 0; j < hand.length; j++){
tokens = hand[j].split("\\s");
if (rank.containsKey(tokens[0])){
((rankCounter)rank.get(tokens[0])).i++;
}
else{
rank.put(tokens[0],new rankCounter());
}
if (suit.containsKey(tokens[2])){
System.out.println("suit if");
System.out.println(tokens[2]);
((suitCounter)rank.get(tokens[2])).i++;->>>>this line
}
else{
System.out.println("suit else");
suit.put(tokens[2],new suitCounter());
}
System.out.println(suit);
is throwing a null pointer exception on the indicated line. The output below shows that it is when an object already exists in the hashmap that it has the problem:
suit else
{Diamonds=1}
suit if
Diamonds
Exception in thread "main" java.lang.NullPointerException
at deckOfCards.compareHand(deckOfCards.java:66)
at deckOfCards.createHand(deckOfCards.java:48)
at deckOfCards.deal(deckOfCards.java:24)
at deckOfCards.main(deckOfCards.java:13)
The hashmap above the one with the problem functions perfectly. I am very new to collections, could someone point me in the right direction?

