-1

I am working on a Evil Hangman Program And for some reason I am getting this error:

What length word do you want to use? 4
How many wrong answers allowed? 4

guesses : 4
guessed : []
current :  ----
Your guess? e
Exception in thread "main" java.lang.NullPointerException
    at Game.HangmanManager.record(HangmanManager.java:142)
    at Game.HangmanMain.playGame(HangmanMain.java:62)
    at Game.HangmanMain.main(HangmanMain.java:42)

Here is my program:

package Game;

import java.util.*;

public class HangmanManager
{

    private String pattern;
    private int max;
    private int length;
    private SortedSet<Character> guessesMade;
    private Set<String> currentWords;
    private Map<String, Set<String>> patternMap;

    public HangmanManager(List<String> dictionary , int length, int max)
    {
        this.max = max;
        this.length = length;


        if( length < 1 && max < 0)
            {
                throw new IllegalArgumentException();

            }

        words = new TreeSet<String>(); 
        guessesMade = new TreeSet<Character>();
        currentWords = new TreeSet<String>(); // current words =(words)
        patternMap = new TreeMap<String, Set<String>>(); // patternMAP = < pattern, words>

        for (String word : dictionary)
        {
            if (word.length() == length)
            {
            words.add(word); // if length of the word matches a word with the same length it will  be added
            }

        }

    }


    public Set<String> words()
    {
        return words;
    }


    public int guessesLeft()
    {
        return max - guessesMade.size();
    }



    public SortedSet<Character> guesses()
    {
        return guessesMade;
    }


    public String pattern()
    {
        if (words.isEmpty())
        {
            throw new IllegalArgumentException("Invalid No Words");
        }
        pattern = " "; // blank for now
        for (int i = 0; i < length; i++)
        {
            pattern += "-"; // will have a "-" for how long the length of the word is 
        }

        return pattern; // will return the number of lines
    }


    public int record(char guess)
    {
          if (guessesLeft() < 1 || words.isEmpty())
          {
                throw new IllegalStateException();  
          }

          if (!words.isEmpty() && guessesMade.contains(guess)) 
          {
                throw new IllegalArgumentException();
          }

          guessesMade.add(guess); // guess
          int occurences = 0;

          for( String word: words)
          {           
              if( patternMap.containsKey (pattern))
              {

                    occurences = generatePattern(word, guess); // the word including the guess letter will fill in the blank spots
                    currentWords.add(word); // the word will be added to the possibilities 
                    currentWords = patternMap.get(pattern); // the word will be able to fill once the guesses are made
//                  if(patternMap.get(pattern)!=null)
//                      {
//                          currentWords = patternMap.get(pattern);
//                          
//                      }
                    patternMap.put(pattern, currentWords);  
              }
              else
              {
                 currentWords.add(word);
                 patternMap.put(pattern, currentWords);
              } 
          }

         words = find();
         return occurences;
    }


    private Set<String> find()
    {
        int maxSize = 0;

        Map <String, Integer> patternCount = new TreeMap<String, Integer>();

        for (String key : patternMap.keySet()) // keyset equals word
        {
            patternCount.put(key, patternMap.get(key).size()); // size of the word

                if (patternMap.get(key).size() > maxSize)
                {
                    maxSize = patternMap.get(key).size();
                    pattern = key; // pattern will becomes based on the word
                } else if (patternMap.get(key).size() == maxSize)
                {
                    if (key.length() >= pattern.length())
                    {
                        pattern = key;
                        maxSize = patternMap.get(key).size(); // the pattern is now the word key 
                    }
                }
            }
        System.out.println("Current pattern: " + pattern);

        return patternMap.get(pattern); // the pattern that will becomes now that the word was picked
    }

    private int generatePattern(String s, char guess) 
    {
        int count = 0;
        pattern = "";
            for (int i = 0; i < length; i++)
            {
                if (s.charAt(i) == guess)
                {
                    pattern += guess + " ";
                    count++;
                } else 
                {
                    pattern += "- ";
                }
            }
        return count;
    }

}

The error seems to happen in the record method for the :

patternMap.put(pattern, currentWords); on Line 149

I ran the debugger numerous times and I do notice that if you follow the program you will see that currentWords becomes Null eventually after the program runs through the words after one time even though I instantiate it and I created a Map for patternMap.

If anyone can tell me what to do or what to change I will really appreciate it because I am so lost with this

MikeCAT
  • 61,086
  • 10
  • 41
  • 58
Jay
  • 29
  • 2
  • 6
  • 3
    Your code is incomplete and won't compile, this means your question is either going to be closed as a duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) or "Why does my code not work - without providing a runnable example" – MadProgrammer Mar 09 '16 at 04:07
  • I can provide the HangmanMain program and dictionary text file – Jay Mar 09 '16 at 04:10

1 Answers1

0

patternMap is a TreeMap. As you said, your problem is with patternMap.put(pattern, currentWords); on line 149. According to the JavaDocs for TreeMap, put() throws NullPointerException...

if the specified key is null and this map uses natural ordering, or its comparator does not permit null keys

Since a TreeMap<String,Object> uses natural ordering of it's keys (i.e. String), the problem is that pattern is null. Why don't you just initialize pattern to a default value:

private String pattern = " ";
Austin D
  • 6,678
  • 2
  • 26
  • 34