Hi there,
I have been trying to make a simple console based Hangman game but I have come across a problem.
I am trying to make the game as Object Orientated as possible as this is what I have the most trouble with and I am trying to learn. The problem is that I am having three null pointer exceptions.
Here are the three classes in problem, when I try and run the code this is the error which I am getting (the program runs but as soon as I enter a userinput this error occurs)
Exception in thread "main" java.lang.NullPointerException
at GenerateWord.hidePhrase(GenerateWord.java:29)
at CheckGuess.CheckGuess(CheckGuess.java:11)
at HangmanTest.main(HangmanTest.java:11)
I have been trying to make a simple console based Hangman game but I have come across a problem.
I am trying to make the game as Object Orientated as possible as this is what I have the most trouble with and I am trying to learn. The problem is that I am having three null pointer exceptions.
public class GenerateWord {
String words[] = new String[23];
{words[1] = "dictionary"; words[2] = "restaurant";words[3] = "television";words[4] = "responsible";words[5] = "technology";
words[6] = "collection";words[7] = "communicate";words[8] = "programming";words[9] = "federation";words[10] = "enterprise";
words[11] = "exaggerate";words[12] = "cappuccino";words[13] = "monkey";words[14] = "computer";words[15] = "information";
words[16] = "systems";words[17] = "poker";words[18] = "bottle";words[19] = "administration";words[20] = "encyclopedia";
words[21] = "textbook";words[22] = "calculus";}
String temp;
public char [] generatedWord;
public char [] hiddenWord;
public char[] genWord(){
Random getRandomWord = new Random();
int n = getRandomWord.nextInt(22);
temp = words[n];
generatedWord = temp.toCharArray();
return generatedWord;
}
public char [] hidePhrase(){
String temp = generatedWord.toString();
String displayPhrase = temp.replaceAll(".", "*");
char [] hiddenPhrase = displayPhrase.toCharArray();
return hiddenPhrase;
}
}
import java.util.*;
public class CheckGuess{
public void CheckGuess() {
Guess guess = new Guess();
GenerateWord gen = new GenerateWord();
char [] wordBin; //Still need to incorporate into program
char[] genWord = gen.generatedWord;
char[] hidWord = gen.hidePhrase();
int guessWordLength = gen.generatedWord.length;
int maxGuesses = 20;
while (maxGuesses > 0 || guessWordLength > 0){
char pGuess = guess.playerGuess();
for (int i = 0; i <genWord.length; i++){
if (genWord[i] == pGuess){
hidWord[i] = pGuess;
maxGuesses -= 1;
guessWordLength -= 1;
}
}
}
}
}
public class HangmanTest {
public static void main(String[] args) {
GenerateWord test = new GenerateWord();
Guess guess = new Guess();
CheckGuess cGuess = new CheckGuess();
test.genWord();
test.hidePhrase();
guess.playerGuess();
cGuess.CheckGuess();
}
}
Here are the three classes in problem, when I try and run the code this is the error which I am getting (the program runs but as soon as I enter a userinput this error occurs)
Exception in thread "main" java.lang.NullPointerException
at GenerateWord.hidePhrase(GenerateWord.java:29)
at CheckGuess.CheckGuess(CheckGuess.java:11)
at HangmanTest.main(HangmanTest.java:11)