“线程‘main’中的异常” java.lang.NullPointerException”
我正在尝试运行一个程序,如果一切顺利的话,能够花一年的时间返回当年发行的专辑的标题。我已经给了它 6 张专辑,现在我正在尝试打印一个标题。我修复了一些非常令人沮丧的错误,但这是我以前从未见过的错误。该错误出现在第 21 行,但我不确定它的含义。有人可以帮忙吗?
package songselector;
import java.util.Scanner;
public class Main {
public class Album
{
int year; String title;
public Album () {
this.year = 0; this.title = null;
}
public Album (int year, String title) {
this.year = year; this.title = title;
}
}
class CAKE {
Album[] albums;
public CAKE () {
albums = new Album[6];
albums[0].year = 1994; albums[0].title = "Motorcade Of Generosity";
albums[1].year = 1996; albums[1].title = "Fashion Nugget";
albums[2].year = 1998; albums[2].title = "Prolonging The Magic";
albums[3].year = 2001; albums[3].title = "Comfort Eagle";
albums[4].year = 2004; albums[4].title = "Pressure Chief";
albums[5].year = 2011; albums[5].title = "Showroom of Compassion";
}
public void printAlbum (int y) {
System.out.println (albums[y].title);
}
}
public static void main(String[] args) {
new Main().new CAKE().printAlbum (0);
}
}
I'm trying to run a program that will, if all goes well, be able to take a year and return the title of an album released in that year. I've given it 6 albums already and I'm now trying to actually print a title. I've fixed a few pretty frustrating errors, but this is one I've not seen before. The error appears at line 21, but I'm not sure what it means. Can anyone help?
package songselector;
import java.util.Scanner;
public class Main {
public class Album
{
int year; String title;
public Album () {
this.year = 0; this.title = null;
}
public Album (int year, String title) {
this.year = year; this.title = title;
}
}
class CAKE {
Album[] albums;
public CAKE () {
albums = new Album[6];
albums[0].year = 1994; albums[0].title = "Motorcade Of Generosity";
albums[1].year = 1996; albums[1].title = "Fashion Nugget";
albums[2].year = 1998; albums[2].title = "Prolonging The Magic";
albums[3].year = 2001; albums[3].title = "Comfort Eagle";
albums[4].year = 2004; albums[4].title = "Pressure Chief";
albums[5].year = 2011; albums[5].title = "Showroom of Compassion";
}
public void printAlbum (int y) {
System.out.println (albums[y].title);
}
}
public static void main(String[] args) {
new Main().new CAKE().printAlbum (0);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这意味着您正在尝试访问/调用空对象上的方法。在您的例子中,您初始化了相册数组,但没有初始化数组中的每个相册。
您需要初始化数组中的每个专辑:
或者更简单(正如@entonio 指出的那样):
因为您有一个合适的构造函数。
还有一件事:不要在每一行中调用多个方法,这将有助于您调试。
It means that you are trying to access / call a method on an object which is null. In your case, you initialized the array of Albums, but didn't initialize each of the albums in the array.
You need to initialize each album in the array:
Or even simpler (as @entonio pointed out):
Since you have a proper constructor.
One more thing: don't call more than one method in each line, it will help you debugging.
当您分配对象数组时,它会填充空值。您需要创建对象来填充它们。您的
albums[0]
尚未创建,因此尝试访问其year
字段(即使是为了写入)会导致 NPE。When you allocate an array of objects, it's filled with null values. You need to create objects to fill them. Your
albums[0]
wasn't created, so trying to access itsyear
field (even for writing) results in a NPE.