4 Replies - 368 Views - Last Post: 25 January 2013 - 07:42 AM Rate Topic: -----

#1 Dowskiii  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 25-January 13

Null Pointer Exception - Basic Hangman Game!

Posted 25 January 2013 - 05:33 AM

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.

 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)

Is This A Good Question/Topic? 0
  • +

Replies To: Null Pointer Exception - Basic Hangman Game!

#2 Flukeshot  Icon User is offline

  • A little too OCD
  • member icon

Reputation: 415
  • View blog
  • Posts: 1,030
  • Joined: 14-November 12

Re: Null Pointer Exception - Basic Hangman Game!

Posted 25 January 2013 - 06:01 AM

A null pointer exception usually indicates that your array index is pointing to an array member which has a 'null' value.

The exception happened on line 29, start there and look for a null reference being used.
Was This Post Helpful? 0
  • +
  • -

#3 ccdan  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 38
  • Joined: 09-December 12

Re: Null Pointer Exception - Basic Hangman Game!

Posted 25 January 2013 - 06:50 AM

There are many issues with your code (no getters/setters, no constructors etc) But the error is generated by the fact that you're trying to access a variable - generatedWord - that only gets initialized if you call the genWord() method from the class GeneratedWord, a thing that you're not doing...

for example
String temp = generatedWord.toString();

should be replaced by:
String temp = genWord().toString();

something similar with this line:
char[] genWord = gen.generatedWord;

replaced by:
char[] genWord2 = gen.genWord(); //this one already return a char array 


notice that a added a 2 after the variable genWord to char array beause it has the same as the method for generating that stuff; you should change its name to something else (it works with the same name but it's bad practice)

one more thing that i noticed, you started assigning the values for array words from index 1 ... arrays begin from index 0...
Was This Post Helpful? 0
  • +
  • -

#4 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1391
  • View blog
  • Posts: 3,077
  • Joined: 05-April 11

Re: Null Pointer Exception - Basic Hangman Game!

Posted 25 January 2013 - 07:23 AM

Normally you would read the words from a file
I suggest you make a file with the words instead, and read it when your program starts

But back to your exceptions
Arrays start at 0 and ends at length-1. Right now you are starting at index 1 instead of 0, which makes words[0] null
There is a high possibility of bugs when filling the array like you are doing it right now. You can instead declare it in the way below, where you don't have to tell the place of the String
String[] words = { "word1", "word2", "word3" };


That should fix your nullpointers :)/>

I see you have named an instance variable temp in your class. This screams for the need of a local variable!
In your genWord method, you want to keep the variable local, so it can't be accessed outside of the method
	public char[] genWord(){
		Random getRandomWord = new Random(); //Local variable
		String word = words[getRandomWord.nextInt(words.length)]; //Local variable
		return word.toCharArray();		
	}



There is nothing wrong with your hidePhrase method, but I would still change it a little bit. No need to use regex or construct a String
	public char[] hidePhrase(char[] word) {
		char[] hidden = new char[word.length];
		Arrays.fill(hidden, '*');
		return hidden;		
	}


This post has been edited by CasiOo: 25 January 2013 - 07:24 AM

Was This Post Helpful? 0
  • +
  • -

#5 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1391
  • View blog
  • Posts: 3,077
  • Joined: 05-April 11

Re: Null Pointer Exception - Basic Hangman Game!

Posted 25 January 2013 - 07:42 AM

I would actually prefer that GenerateWord has no instance variables at all, at least not the generatedWord and hiddenPhrase instance variables
The methods called on GenerateWord returns char arrays, which you should be holding onto and use. There is no reason to keep them as instance variables

I also suggest you think about the responsibilities of your classes
If you rename GenerateWord to WordGenerator, then you get a clear idea of what the class is supposed to be doing by just reading the class name
You have a class and a method named CheckGuess which is kinda odd. I would expect the method to check the given guess against the previously generated word, but why the class name CheckGuess? You might want to reconsider that

I like to start out with interfaces when I code, it just gives me a good overview of how the classes are supposed to work together
public interface SecretWord {
    //Returns true if the letter is in the word to guess, and has not yet been revealed
    public boolean reveal(char letter);

    //Makes a guess on a word. Returns if word matches the hidden word
    public boolean guess(String word);
}


This post has been edited by CasiOo: 25 January 2013 - 07:43 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1