2

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.

iknow
  • 8,358
  • 12
  • 41
  • 68
Monty Roob
  • 23
  • 4
  • 3
    Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Arun Sudhakaran Jul 22 '20 at 05:11
  • 1
    You can solve your problem by initializing all arrays like so `String[] userNames = new String[5] // size of 5`. But why exactly do you need 2 scanners? – René Jul 22 '20 at 06:02
  • Well after I used the first scanner more than two times it stopped working. I just assumed that’s how scanners are, but maybe the IDE I was using wasn’t working right. Thanks for pointing that out! – Monty Roob Jul 23 '20 at 14:24

4 Answers4

2

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.

Chathura Buddhika
  • 2,067
  • 1
  • 21
  • 35
  • this is not gonna pass the whole code without error! – user404 Jul 22 '20 at 05:16
  • If I'm not mistaken he is asking "What can I do to solve this NullPointerException error in Java?". TL;DR; You have to initialize all arrays. – Chathura Buddhika Jul 22 '20 at 05:18
  • to narrow down the question, you will be right but Again, the person said that because s/he does not know the actual problem. S/he is just describing here what s/he sees in bare eyes. So, after that s/he is going to face that again! Do you want that? I guess NO! – user404 Jul 22 '20 at 05:20
  • 1
    No I do understand. I didn’t initialize the arrays so it was giving the error. The answer was just giving an example of what I need to do for all my arrays. – Monty Roob Jul 23 '20 at 14:27
1

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


}
Kavitha Karunakaran
  • 1,340
  • 1
  • 17
  • 32
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;

0

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

user404
  • 1,934
  • 1
  • 16
  • 32