为什么我会得到 Java 的空指针异常(NullPointerException)?

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

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。如果需要进一步的帮助,请提供有关 CourseStudent 类的构造函数和其他相关代码的更多信息。

英文:

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 &quot;s+=c.getName() + &quot; &quot; +... &quot;

this is a method in Student.

public String getCourses()
	{
		String s = &quot;&quot;;
		for(Course c: this.courses)
		{
			s+=c.getName() + &quot; &quot; + c.getID() + &quot; &quot; + c.getScore(this.id);
			s+=&quot;\n&quot;;
		}
		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 &quot;main&quot; 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 &lt; 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:

答案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"

huangapple
  • 本文由 发表于 2020年10月26日 09:57:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/64530603.html
匿名

发表评论

匿名网友

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

确定