“线程‘main’中的异常” java.lang.NullPointerException”

发布于 2024-11-06 11:45:17 字数 1080 浏览 0 评论 0原文

我正在尝试运行一个程序,如果一切顺利的话,能够花一年的时间返回当年发行的专辑的标题。我已经给了它 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光暖心i 2024-11-13 11:45:17

这意味着您正在尝试访问/调用空对象上的方法。在您的例子中,您初始化了相册数组,但没有初始化数组中的每个相册。

您需要初始化数组中的每个专辑:

albums = new Album[6];
albums[0] = new Album();
albums[0].year = 1994; 
albums[0].title = "Motorcade Of Generosity";
...

或者更简单(正如@entonio 指出的那样):

albums = new Album[6];
albums[0] = new Album(1994, "Motorcade Of Generosity");
albums[1] = new Album(1996, "Fashion Nugget");
...

因为您有一个合适的构造函数。

还有一件事:不要在每一行中调用多个方法,这将有助于您调试。

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:

albums = new Album[6];
albums[0] = new Album();
albums[0].year = 1994; 
albums[0].title = "Motorcade Of Generosity";
...

Or even simpler (as @entonio pointed out):

albums = new Album[6];
albums[0] = new Album(1994, "Motorcade Of Generosity");
albums[1] = new Album(1996, "Fashion Nugget");
...

Since you have a proper constructor.

One more thing: don't call more than one method in each line, it will help you debugging.

绝情姑娘 2024-11-13 11:45:17

当您分配对象数组时,它会填充空值。您需要创建对象来填充它们。您的 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 its year field (even for writing) results in a NPE.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文