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);
}
}