-3

I'm making a hangman game. However, I am struggling when trying to output the word that the player has to guess but as blank characters e.g. if the word was 'great', the program would output _ _ _ _ _

I've used a for loop to create an array that has the same amount of characters as the word, but whenever I run my code, I get an java.lang.NullPointerException.

I have tried researching the issue and looking in the java documentation, however I am new to the language and find it hard to make sense of the issue and how to resolve it. Where am I going wrong?

import java.util.*;

public class Hangman 
{
    static char[] word1 = {'a', 'm', 'a', 'z', 'i', 'n', 'g'};
    static char[] word2 = {'f', 'a', 'b', 'u', 'l', 'o', 'u', 's'};
    static char[] word3 = {'g', 'r', 'e', 'a', 't'};
    static Random random = new Random();
    static int choice;
    static char[] choice_array;
    static char[] num_of_spaces;

    public static void main(String args[])
    {
        System.out.println("-------");
        System.out.println("Hangman");
        System.out.println("-------");
        choice = random.nextInt(3)+1;
        switch (choice)
        {
        case 1:
            choice_array = word1;
            break;
        case 2:
            choice_array = word2;
            break;
        case 3:
            choice_array = word3;
            break;
        }
        System.out.println(choice_array);
        for(int counter = 0; counter < choice_array.length; counter++)
        {
            num_of_spaces[counter] = '_';
        }
        System.out.println(num_of_spaces);
    }
}
Elliott Frisch
  • 191,680
  • 20
  • 149
  • 239
SHatna
  • 37
  • 6

3 Answers3

1

You never initialized num_of_spaces. Do so before the loop. Like,

System.out.println(choice_array);
num_of_spaces = new char[choice_array.length];
for(int counter = 0; counter < choice_array.length; counter++)
{
    num_of_spaces[counter] = '_';
}

or

 num_of_spaces = new char[choice_array.length];
 Arrays.fill(num_of_spaces, '_');

And to print the array you will want Arrays.toString(char[]) like

System.out.println(Arrays.toString(num_of_spaces));

or

System.out.println(new String(num_of_spaces));
Elliott Frisch
  • 191,680
  • 20
  • 149
  • 239
1

The array num_of_spaces is never initialized. So at the time of assigning value to it using [counter] it is not allocated space and doesn't know how many items it can store.

Try num_of_spaces = new char[choice_array.length];. That should solve.

Raman Shrivastava
  • 2,905
  • 13
  • 26
0

Try this. since you are new to java, I've pasted the whole program with fix.

Solution: All you need is to add below if condition to your for loop. This is need since you just declared num_of_spaces variable but not not initialized. if you not initilized a variable and try to access it java always return NullpointerException.

if(num_of_spaces==null){
    num_of_spaces = new char[choice_array.length];
}

After fix, you program should look like below.

import java.util.Random;

public class Hangman
{
    static char[] word1 = {'a', 'm', 'a', 'z', 'i', 'n', 'g'};
    static char[] word2 = {'f', 'a', 'b', 'u', 'l', 'o', 'u', 's'};
    static char[] word3 = {'g', 'r', 'e', 'a', 't'};
    static Random random = new Random();
    static int choice;
    static char[] choice_array;
    static char[] num_of_spaces;

    public static void main(String args[])
    {
        System.out.println("-------");
        System.out.println("Hangman");
        System.out.println("-------");
        choice = random.nextInt(3)+1;
        switch (choice)
        {
            case 1:
                choice_array = word1;
                break;
            case 2:
                choice_array = word2;
                break;
            case 3:
                choice_array = word3;
                break;
        }
        System.out.println(choice_array);
        for(int counter = 0; counter < choice_array.length; counter++)
        {
            if(num_of_spaces==null){
                num_of_spaces = new char[choice_array.length];
            }
            num_of_spaces[counter] = '_';
        }
        System.out.println(num_of_spaces);
    }
}

Output:


Hangman

great


Kumar
  • 905
  • 6
  • 23