英文:
why am i getting a NullPointerException Java
问题
这是一个Java编程问题,你有两个类和一个使用这两个类的测试类。在EclipseIDE中运行时,它一直告诉你在 s+=c.getName() + " " +...
这一行有一个NullPointerException。
这是Student类中的一个方法:
public String getCourses() {
String s = "";
for (Course c : this.courses) {
s += c.getName() + " " + c.getID() + " " + c.getScore(this.id);
s += "\n";
}
return s;
}
它调用了Course类中的这个方法:
public String getID() {
return this.id;
}
当只调用getName()
时没有问题,但一旦添加getID()
就出现了问题。getName()
是相同类型的代码,它将对象的 "name" 作为字符串返回。
"name" 和 "id" 是通过构造函数进行初始化的。
堆栈跟踪如下:
Exception in thread "main" java.lang.NullPointerException
at hw6.Student.getCourses(Student.java:47)
at hw6.CourseStudent_test.main(CourseStudent_test.java:100)
这是 getScore
方法:
public double getScore(String id) {
int i = 0;
for (; i < this.students.length; i++) {
if (this.students[i].getID() == id) {
break;
}
}
return this.scores[i];
}
请检查你的代码以确保 courses
数组和 students
数组都已正确初始化,以及确保没有空引用导致NullPointerException。如果需要进一步的帮助,请提供有关 Course
和 Student
类的构造函数和其他相关代码的更多信息。
英文:
So im writing a Java programming, I have 2 classes, and a class test that uses these 2 classes. running eclipseIDE, it keeps telling me i have a NullPointerException at the "s+=c.getName() + " " +... "
this is a method in Student.
public String getCourses()
{
String s = "";
for(Course c: this.courses)
{
s+=c.getName() + " " + c.getID() + " " + c.getScore(this.id);
s+="\n";
}
return s;
}
it calls this method that is in the Course class.
public String getID()
{
return this.id;
}
i tried only doing getName(); it had no issue, however once i added getID() it became an issue. getName is the same type of code, it returns the "name" of the object as a string.
name and id is "initialized" via a constructor
Stacktrace:
Exception in thread "main" java.lang.NullPointerException
at hw6.Student.getCourses(Student.java:47)
at hw6.CourseStudent_test.main(CourseStudent_test.java:100)
this is the getScore method
public double getScore(String id)
{
int i = 0;
for(; i < this.students.length;i++)
{
if(this.students[i].getID() == id )
{
break;
}
}
return this.scores[i];
}
答案1
得分: 2
如果异常发生在您的堆栈跟踪+问题所指示的位置,那么courses
中的一个元素必须是null
。
推理:
-
如果异常是在
Course
方法调用的其中一个内部抛出的,那么堆栈跟踪将显示该方法作为顶部堆栈帧。 -
如果这三个调用中的某个返回了
null
,您将不会得到空指针异常。在结果的连接中,您只会得到"null";请参阅https://stackoverflow.com/questions/4199298/string-concatenation-with-null。
英文:
If the exception is occurring where your stacktrace + question says it is, then one of the elements in courses
must be null
.
Reasoning:
-
If the exception was thrown inside one of the
Course
method calls, then the stacktrace would show that method as the top stack frame. -
If one of those 3 calls returned
null
, you wouldn't get an NPE. You would just get "null" in the resulting concatenation; see https://stackoverflow.com/questions/4199298/string-concatenation-with-null.
答案2
得分: 0
发现问题,由于 "For each" 循环,当它遇到一个空对象时,它尝试运行 getter 方法,然而每次都返回 null,因为该对象内没有每个变量的 "value",所以我只是添加了一个 if 条件来检查对象是否为 "null"。
英文:
Found the issue, Because of the "For each " loop, when it gets to an Null object, it tries to run the getter methods, however each time it returns null, as there is no "value" for each variable within that object, so i just added a if condition to check if the object is "null"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论