Java 对象中的空指针异常错误

Java null pointer Exception Error in Objects

我编写了一个包含对象的程序,但在尝试 运行 时出现 NullPointerException 错误,但我无法发现我的代码的确切问题。在这里(粗线是日食指向错误的地方):

public class Cinema {
    private String cinemaName;
    private String Address;
    private Room Rooms[];
    private Movie Movies[];
    private final int MAX_NUM_MOVIES = 20;
    private final int MAX_NUM_ROOMS = 5;
    private int movieCounter;
    private int roomCounter;

    public Cinema(String cinemaName, String Address) {
        this.cinemaName = cinemaName;
        this.Address = Address;
        Room Rooms[] = new Room[MAX_NUM_ROOMS];
        @SuppressWarnings("unused")
        Movie Movies[] = new Movie[MAX_NUM_MOVIES];
        movieCounter = 0;
        roomCounter = 0;
    }

    public boolean addMovieToCinema (Movie theMovie) {
        if (theMovie == null) {
            return false;
        }
        if (movieCounter < MAX_NUM_MOVIES) {
            Movies[movieCounter] = new Movie(theMovie);
            movieCounter++;
            return true;
        } else
            return false;
    }
}

有什么想法吗?

而不是赋值给这两个成员变量:

private Room Rooms[];
private Movie Movies[];

你用这两行声明了新的局部变量:

Room Rooms[] = new Room[MAX_NUM_ROOMS];
Movie Movies[] = new Movie[MAX_NUM_MOVIES];

修复它们以初始化您的 RoomsMovies class 成员:

Rooms = new Room[MAX_NUM_ROOMS];
Movies = new Movie[MAX_NUM_MOVIES];

这两个数组应该是class字段

private int movieCounter;
private int roomCounter;
private Room[] Rooms = new Room[MAX_NUM_ROOMS];
private Movie[] Movies = new Movie[MAX_NUM_MOVIES];

public Cinema(String cinemaName, String Address) {
  this.cinemaName = cinemaName;
  this.Address = Address;
  movieCounter = 0;
  roomCounter = 0;
}

为什么不使用列表来存储电影(和房间)?您不必提供硬编码限制。

List<Movie> movies = new ArrayList<>();

public boolean addMovieToCinema(Movie theMovie) {
    if (theMovie == null) {
        return false;
    }
    movies.add( theMovie );
    return true;
}

一切都变得简单。

问题是您的构造函数隐藏了您的 class 成员。

如果你声明 private Movie Movies[]; 那么在你的构造函数中你应该写:Movies = new Movie[MAX_NUM_MOVIES];

当你写:Movie Movies[] = new Movie[MAX_NUM_MOVIES]; 那么你实际上声明了一个新变量,它是构造函数的局部变量,这就是为什么 class 成员 Movies 为空,因为它不是谁被初始化了

而不是定义这个:

private Room Rooms[];
private Movie Movies[];

为什么你只定义一个集合!?喜欢:

private Collection<Room> rooms;
private Collection<Movie> movies;

然后,在您的构造函数中,您必须初始化您的集合:

this.rooms = new ArrayList<>();
this.movies = new ArrayList<>();

我会说这是 Java 方式。还要记住,属性名称应该是小写的。

我认为错误出在您的 Cinema class 构造函数中。 它应该看起来像这样:

   public Cinema(String cinemaName, String Address) {
    this.cinemaName = cinemaName;
    this.Address = Address;
// Remove type before variable to initialize class attribute instead of declaring a new varialble
    Rooms = new Room[MAX_NUM_ROOMS];
    @SuppressWarnings("unused")
// Remove type before variable to initialize class attribute instead of declaring a new varialble
    Movies = new Movie[MAX_NUM_MOVIES];
    movieCounter = 0;
    roomCounter = 0;
}

Class 属性将被初始化并且 NullpointerException 应该消失。

干杯, 塞巴斯蒂安

一种选择是在声明数组时分配内存:

private Room[] Rooms = new Room[MAX_NUM_ROOMS];

或者,如果您想在构造函数中分配内存,您需要:

Rooms = new Room[MAX_NUM_ROOMS];