Question: Question: my code keeps giving me an error that says: Exception in thread main java.lang.IllegalStateException: Active words is null! is there any way to fix

Question: my code keeps giving me an error that says: Exception in thread "main" java.lang.IllegalStateException: Active words is null! is there any way to fix this?
import java.util.Set;
import java.util.TreeMap;
import java.util.ArrayList;
import java.util.Collections;
/**
* Manages the details of EvilHangman. This class keeps
* tracks of the possible words from a dictionary during
* rounds of hangman, based on guesses so far.
*
*/
public class HangmanManager {
// instance variables / fields
ArrayList activeWords;
private Set dictionary;
private int numGuessesAllowed;
private ArrayList guessesMade;
private HangmanDifficulty difficulty;
private String pattern;
private int numWrongGuesses;
/**
* Create a new HangmanManager from the provided set of words and phrases.
* pre: words != null, words.size()>0
* @param words A set with the words for this instance of Hangman.
* @param debugOn true if we should print out debugging to System.out.
*/
public HangmanManager(Set words, boolean debugOn){
if (words == null || words.size()<=0){
throw new IllegalArgumentException("Invalid set of words!");
}
dictionary = words;
}
/**
* Create a new HangmanManager from the provided set of words and phrases.
* Debugging is off.
* pre: words != null, words.size()>0
* @param words A set with the words for this instance of Hangman.
*/
public HangmanManager(Set words){
this(words, false);
}
/**
* Get the number of words in this HangmanManager of the given length.
* pre: none
* @param length The given length to check.
* @return the number of words in the original Dictionary
* with the given length
*/
public int numWords(int length){
int count =0;
for (String word : dictionary){
if (word.length()== length){
count++;
}
}
return count;
}
/**
* Get for a new round of Hangman. Think of a round as a
* complete game of Hangman.
* @param wordLen the length of the word to pick this time.
* numWords(wordLen)>0
* @param numGuesses the number of wrong guesses before the
* player loses the round. numGuesses >=1
* @param diff The difficulty for this round.
*/
public void prepForRound(int wordLen, int numGuesses, HangmanDifficulty diff){
// Resets variables to starting values to start new round
numGuessesAllowed = numGuesses;
numWrongGuesses =0;
guessesMade = new ArrayList<>();
difficulty = diff;
// Fills active words list with words with inputted length
activeWords = new ArrayList<>();
fillWithGivenLength(wordLen);
pattern ="";
// Fills pattern with "-" to represent unknown letters
for (int i =0; i < wordLen; i++){
pattern +='-';
}
}
/**
* The number of words still possible (live) based on the guesses so far.
* Guesses will eliminate possible words.
* @return the number of words that are still possibilities based on the
* original dictionary and the guesses so far.
*/
public int numWordsCurrent(){
//System.out.println("HERE"+ activeWords);
if(activeWords == null){
throw new IllegalStateException("Active words is null!");
}
return activeWords.size();
}
/**
* Get the number of wrong guesses the user has left in
* this round (game) of Hangman.
* @return the number of wrong guesses the user has left
* in this round (game) of Hangman.
*/
public int getGuessesLeft(){
return numGuessesAllowed - numWrongGuesses;
}
/**
* Return a String that contains the letters the user has guessed
* so far during this round.
* The characters in the String are in alphabetical order.
* The String is in the form [let1, let2, let3,... letN].
* For example [a, c, e, s, t, z]
* @return a String that contains the letters the user
* has guessed so far during this round.
*/
public String getGuessesMade(){
StringBuilder s = new StringBuilder();
s.append("[");
// If user has made any guesses
if (guessesMade.size()>0){
Collections.sort(guessesMade);
for (int i =0; i < guessesMade.size()-1; i++){
s.append(guessesMade.get(i)+",");
}
// Append last element
s.append(guessesMade.get(guessesMade.size()-1));
}
// Close off String
s.append("]");
String string = s.toString();
return string;
}
/**
* Check the status of a character.
* @param guess The characater to check.
* @return true if guess has been used or guessed

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!