What can I do to solve this NullPointerException error in Java?

Viewed 66

I am making this very basic profile creator in java and as I was debugging I kept ending up with a NullPointerException error. I don't know how to solve this and I would appreciate it if someone could please tell me how to solve this. Here is the code:

import java.util.Scanner;

public class Creator
{
    String[] userNames;
    int[] birthYears;
    String[] genders;
    String userCommand;

    Scanner scanner1 = new Scanner(System.in);
    Scanner scanner2 = new Scanner(System.in);

    public void create()
    {
        System.out.println("What is your name?");
        userNames[0] = scanner1.nextLine();
        System.out.println(userNames[0]);

        System.out.println("In what year were you born?");
        birthYears[0] = scanner1.nextInt();
        System.out.println(birthYears[0]);

        System.out.println("What is your gender?");
        genders[0] = scanner2.nextLine();
        System.out.println(genders[0]);
    }
}

In the main method, I just created a Creator object and used it to call the method create(). The code never gets past the "What's your name?" block of code and every time I run it I end up with this error:

Exception in thread "main" java.lang.NullPointerException
at Creator.create(Creator.java:19)

The error is referencing to this line:

userNames[0] = scanner1.nextLine();

I have no idea how to fix this and I would appreciate it if someone told me how to fix it.

4 Answers

When an array is declared, only a reference variable is created. Unlike primitive class variables, arrays are not instantiated while creating the object. To actually create or give memory to the array, you have to create an array like this;

String[] userNames = new String[5];

Otherwise, you will get NullPointerException at the runtime while trying to access the array, since there is no actual array object attached to it.

This is happening because you are just declaring the arrays named userNames, birthYears and genders and not initializing them. NullPointerException occurs when you try to access an object or its methods without initializing it. Make it a point to do all the initializations inside the Creator class's constructor.

import java.util.Scanner;

public class Creator {

String[] userNames;
int[] birthYears;
String[] genders;
String userCommand = "";

Scanner scanner1;
Scanner scanner2;
// initialize inside default constructor.
public Creator(){
   userNames[] = new String[10];
   birthYears[] = new int[10];
   genders[] = new String[10]
   scanner1 = new Scanner(System.in);
   scanner2 = new Scanner(System.in);

}


public void create() {
    
    System.out.println("What is your name?");
    userNames[0] = scanner1.nextLine();
    System.out.println(userNames[0]);
    
    System.out.println("In what year were you born?");
    birthYears[0] = scanner1.nextInt();
    System.out.println(birthYears[0]);
    
    System.out.println("What is your gender?");
    genders[0] = scanner2.nextLine();
    System.out.println(genders[0]);
    
    
}


}

You has not initialized the arrays. You need create the arrays 'cause position 0 not exists and for these reason you has a NullPointerException.

String[] userNames = new String[5]; // for example to store 5 usernames

And you need create the other arrays too;

What you need is initializing your arrays. And you can do it like this, considering their required size as 1.:

String[] userNames = new String[1];
 int[] birthYears = new int[1];
 String[] genders = new String[1];

Now you are ok to go.
Without initializing them, it means no memory is allocated for those variables and hence you got that NPE error

Related