在Java中出现空指针异常,尽管已经初始化了变量。

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

Null pointer exception in Java despite having initialized the varibale

问题

Here is the translated code part:

public class GradeClass {
    
    String nameF;
    int[] gradesF = new int[4];
    float average;
    
    public GradeClass(String name, int grade1, int grade2, int grade3, int grade4) {
        nameF = name;
        gradesF[0] = grade1;
        gradesF[1] = grade2;
        gradesF[2] = grade3;
        gradesF[3] = grade4;
    }
    
    public float getAverage() {
        average = (gradesF[0] + gradesF[1] + gradesF[2] + gradesF[3] / gradesF.length);
        return average;
    }
    
    public String getName() {
        return nameF;
    }
    
    public int[] getGrades() {
        return gradesF;
    }
}
    
else if (a == 2) {
    System.out.println("Enter name of " + (a+1) + " person. :: ");
    nameI = keyboard.next();
    
    while (size < 1 || size > 4) {
        System.out.println("How many grades would you like to enter? 'Max-4' :: ");
        size = keyboard.nextInt();
    }
    
    while (gradeArray[b] > 1 || gradeArray[b] > 100) {
        System.out.println("Enter the " + size + " grades here :: ");
        for (b = 0; b < size; b++) {
            gradeArray[b] = keyboard.nextInt();
        }
        GradeClass student3 = new GradeClass(nameI, gradeArray[0], gradeArray[1], gradeArray[2], gradeArray[3]);
    }
}
else {
    numPersons = 0;
}

grade1A = student1.gradesF[0] + student2.gradesF[0] + student3.gradesF[0];

averageA = student1.average + student2.average + student3.average;

System.out.println("Name \t\t Grade 1 \t Grade 2 \t Grade 3 \t Grade 4 \t Average \n");
System.out.println(student1.getName() + "\t" + student1.gradesF[0] + "\t" + student1.gradesF[1] + "\t" + student1.gradesF[2] + "\t" + student1.gradesF[3] + student1.average + "\n");
System.out.println("Average\t\t" + grade1A + "\t" + grade2A + "\t" + grade3A + "\t" + grade4A + "\t" + averageA + "\n");

Note: I've removed the HTML escape codes like &quot; to make the code more readable. Please make sure to handle input and output properly in your Java application to avoid issues like NullPointerException.

英文:
public class GradeClass {
String nameF;
int[] gradesF = new int[4];
float average;
public GradeClass(String name, int grade1, int grade2, int grade3, int grade4) {
nameF = name;
gradesF[0] = grade1;
gradesF[1] = grade2;
gradesF[2] = grade3;
gradesF[3] = grade4;
}
public float getAverage() {
average = (gradesF[0] + gradesF[1] + gradesF[2] + gradesF[3] / gradesF.length);
return average;
}
// Used to reference number of sides used in current state of die.
public String getName() {
return nameF;
}
// Used to reference number of sides used in current state of die.
public int[] getGrades() {
return gradesF;
}	

}

   else if (a == 2) {
System.out.println(&quot;Enter name of  &quot;+ (a+1)  +&quot; person. :: &quot;);
nameI = keyboard.next();
while (size &lt; 1 || size &gt; 4 ) {
System.out.println(&quot;How many grades would you like to enter? &#39;Max-4&#39; :: &quot;);
size = keyboard.nextInt();
}
while (gradeArray[b] &gt; 1 || gradeArray[b] &gt; 100 ) {
System.out.println(&quot;Enter the &quot; + size + &quot; grades here :: &quot;);
for(b=0; b&lt;size; b++) {
gradeArray[b] = keyboard.nextInt();
}
GradeClass student3 = new GradeClass(nameI, gradeArray[0],gradeArray[1],gradeArray[2],gradeArray[3]);
}	}	
else {
numPersons = 0;
}
}
grade1A = student1.gradesF[0] + student2.gradesF[0] + student3.gradesF[0];
averageA = student1.average + student2.average + student3.average;
System.out.println(&quot;Name \t\t Grade 1 \t Grade 2 \t Grade 3 \t Grade 4 \t Average \n&quot;);
System.out.println(student1.getName() + &quot;\t&quot; + student1.gradesF[0] +&quot;\t&quot; +student1.gradesF[1]+&quot;\t&quot; +student1.gradesF[2] +&quot;\t&quot; + student1.gradesF[3] + student1.average + &quot;\n&quot;);
System.out.println(&quot;Average\t\t&quot; + grade1A + &quot;\t&quot; + grade2A + &quot;\t&quot;  + grade3A + &quot;\t&quot;  + grade4A + &quot;\t&quot; + averageA + &quot;\n&quot;);
}}

In my main im trying to call on another instance of a class. What it needs to do is take number of participants, there names, how many grades they have, their grades, enter all grades into an array, send the name along with the grades to gradeClass and allow for me to call and use them from there.

    gradeClass student1 = new gradeClass(nameI, gradeArray[0],gradeArray[1],gradeArray[2],gradeArray[3]);

when i try to use this it tells me i need a variable

    gradeClass student1;
student1 = new gradeClass(nameI, gradeArray[0],gradeArray[1],gradeArray[2],gradeArray[3]);

then it tells me to initialize student1 so

    gradeClass student1 = null;

from here i get "java.lang.NullPointerException

any ideas? Error is being called at

    student1 = new gradeClass(nameI, gradeArray[0],gradeArray[1],gradeArray[2],gradeArray[3]);

答案1

得分: 3

首先,类名应以大写字符开头,遵循Java的约定。

尽管已经初始化了变量

int[] gradesF;

您已经定义了一个变量,但变量是null,因为您没有指定数组的大小。

gradesF[0] = grade1;
gradesF[1] = grade2;
gradesF[2] = grade3;
gradesF[3] = grade4;

您不能在没有首先定义数组大小的情况下尝试给数组分配值。

您可以使用以下代码定义一个数组:

int[] gradesF = new int[4];

现在,您可以使用您上面提到的代码为数组分配4个值。

编辑:

gradeClass student1;
student1 = new gradeClass(nameI, gradeArray[0],gradeArray[1],gradeArray[2],gradeArray[3]);

首先,没有必要在两个语句中执行此操作,因此大多数人会使用以下方式:

gradeClass student1 = new gradeClass(nameI, gradeArray[0], gradeArray[1], gradeArray[2], gradeArray[3]);

但问题是,在您发布的代码中,您从未创建一个名为 "gradeArray" 的变量并将其初始化为一个值。您不能在没有先声明它的情况下使用变量。

英文:

First of all class names should start with an upper case character. Follow Java conventions.

> despite having initialized the varibale

int[] gradesF;

You have defined a variable, but the variable is null because you have not specified the size of the array.

    gradesF[0] = grade1;
gradesF[1] = grade2;
gradesF[2] = grade3;
gradesF[3] = grade4;

You can't just attempt to assign values to the Array without first defining the size of the Array.

You define an Array with code like:

int[] gradesF = new int[4];

Now you can assign the 4 values in the array, using the code you have above.

Edit:

gradeClass student1;
student1 = new gradeClass(nameI, gradeArray[0],gradeArray[1],gradeArray[2],gradeArray[3]);

First of all there is no need to do that in two statements, so most people would do:

gradeClass student1 = new gradeClass(nameI, gradeArray[0], gradeArray[1], gradeArray[2], gradeArray[3]);

But the problem is that in the code you posted you never create a "gradeArray" variable and initialize it with a value. You can't just use a variable without first declaring it.

答案2

得分: -1

"ok i figured out what was wrong, the GradeClass student3 = new GradeClass(nameI, gradeArray[0],gradeArray[1],gradeArray[2],gradeArray[3]); was inside a if/else statement and wouldn't allow me to call for it outside it. I had to send it to the class inside the if/else statement and then call for it again inside the if/else statement and assign it to a new variable. Thanks for the help!"

「好的,我找到了问题所在,GradeClass student3 = new GradeClass(nameI, gradeArray[0],gradeArray[1],gradeArray[2],gradeArray[3]); 这段代码位于一个if/else语句内,不允许我在外部调用它。我不得不将它发送到if/else语句内的类中,然后再次在if/else语句内调用它并将其分配给一个新变量。感谢您的帮助!」

英文:

ok i figured out what was wrong, the
GradeClass student3 = new GradeClass(nameI, gradeArray[0],gradeArray[1],gradeArray[2],gradeArray[3]);

was inside a if/else statement and wouldn't allow me to call for it outside it. I had to send it to the class inside the if/else statement and then call for it again inside the if/else statement and assign it to a new variable. Thanks for the help!

huangapple
  • 本文由 发表于 2020年8月6日 05:53:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/63274059.html
匿名

发表评论

匿名网友

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

确定