This question already has answers here :
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));
How to resolve the java.lang.NullPointerException, is thrown when a reference variable is accessed (or de-referenced) and is not pointing to any object. This error can be resolved by using a try-catch block or an if-else condition to check if a reference variable is null before dereferencing it. An example of null array. In this example, the java.lang.NullPointerException is raised as the length of a String array is accessed which is set as null. This is how the String array is declared: String anArray[] = null; As I try to display the array length by using the System.out.println, the exception is raised as follows: See online demo and code
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.
java.lang.nullpointerexception error and fixing with 3 examples, RuntimeException , Exception , or Throwable may unintentionally trap other exception types and prevent them from being handled properly. NullPointerException is very easy to find, just look at trace and find out which Class has got the nullpointer exceptions. You should see a line number in front of the class name. Next, look at the code and exact line and check which object can be null, which causes a NullPointerException exception.
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
What is a NullPointerException, and how do I fix it?, Till you have created the ResultSet Object but every index is empty that is pointing to null reference that is the reason you are getting null. So just assign the Object on that index and then set the value. Output. Exception in thread “main” java.lang.NullPointerException at Third.main(Third.java:6) Case 4: Referring other class Data-Members. If we create an object of another class and try to access the uninitialized data members of that class, then it will raise null pointer exception.
ERR08-J. Do not catch NullPointerException or any of , NullPointerException is an error in Java that occurs as a Java program attempts to An example of generating java.lang. java-lang-nullpointerexception array In Java arrays are reference types just like classes, therefore, the scenarios where NullPointerException occurs are almost similar. While working with arrays a NullPointerException occurs − If you try to access the elements of an array which is not initialized yet (which is null).
NullPointerException when Creating an Array of objects, In Java there is default value for every type, when you don't initialize the instance variables of Related Questions & Answers; How to create a thread without implementing the Runnable interface in Java? Exception in thread "main" java.lang. While working with arrays a NullPointerException occurs −. Taking the length of null, as if it were an array. Accessing or modifying the slots of null object, as if it were an array. Throwing null, as if it were a Throwable value. When you try to synchronize over a null object. Why do we need the null value? Null is a special value used in Java.
When does a Java Array Throws a NullPointerException?, java.lang.NullPointerException, HTTP Error 500 internal server error, array; 1.7 NullPointerException when accessing index value of null array; 1.8 java.lang. 4. Arrays are the main culprits of Null Pointer Exception. Hence, make it a practice to initialize the arrays immediately after their declaration. 5. Try and make a practice of declaring variables just where you are using them. This will help avoid instances of declaring variables with Null values. Page Last Updated: January 2014
Comments Could we have the full stacktrace (Error that is thrown)? Exception in thread "main" java.lang.NullPointerException at Hangman.main(Hangman.java:33)
You never initialize num_of_spaces
where have you initialized this array static char[] num_of_spaces;
? Thanks for the help Eliott, I didn't know that I would have to initialise the array before using it. @SHatna You might also consider static char[] word1 = "amazing".toCharArray();