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.