我该如何解决Java中的NullPointerException错误?

huangapple go评论95阅读模式
英文:

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

问题

在Java中,您的代码中出现了NullPointerException错误。这个错误通常是由于尝试访问一个空对象或未初始化的变量而引起的。在您的代码中,数组和字符串变量没有被初始化,因此在尝试访问它们时会引发NullPointerException错误。要解决这个问题,您需要为这些变量分配内存空间或初始化它们。以下是您的修复代码:

import java.util.Scanner;

public class Creator
{
    String[] userNames = new String[1];
    int[] birthYears = new int[1];
    String[] genders = new String[1];
    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]);
    }

    public static void main(String[] args)
    {
        Creator creator = new Creator();
        creator.create();
    }
}

在这个修复后的代码中,我为userNamesbirthYearsgenders数组分配了一个元素的内存空间,以确保它们不再为空。这应该解决您的NullPointerException错误。

英文:

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.

答案1

得分: 2

当声明数组时,只会创建一个引用变量。与原始类变量不同,在创建对象时不会实例化数组。要实际创建或分配内存给数组,您必须像这样创建一个数组;

String[] userNames = new String[5];

否则,当尝试访问数组时,您将在运行时收到NullPointerException,因为它没有与实际的数组对象关联。

英文:

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.

答案2

得分: 1

这是因为您只是声明了名为userNames、birthYears和genders的数组,但没有初始化它们。当您尝试在未初始化对象或其方法的情况下访问它们时,会发生NullPointerException。请确保在Creator类的构造函数内进行所有初始化。

import java.util.Scanner;

public class Creator {

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

Scanner scanner1;
Scanner scanner2;
// 在默认构造函数内进行初始化。
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]);
    
    
}


}
英文:

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


}

答案3

得分: 0

你尚未初始化数组。您需要创建数组,因为位置0不存在,因此您会遇到NullPointerException异常。

String[] userNames = new String[5]; // 例如,用于存储5个用户名

您还需要创建其他数组;

英文:

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;

答案4

得分: 0

你需要的是初始化你的数组。你可以像这样做,考虑它们所需的大小为1:

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

现在你可以继续了。

没有初始化它们,意味着没有为这些变量分配内存,因此你会得到NPE错误。

英文:

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. <br>
Without initializing them, it means no memory is allocated for those variables and hence you got that NPE error

huangapple
  • 本文由 发表于 2020年7月22日 12:57:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/63027160.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定