英文:
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 "
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("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");
}}
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!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论