Jam*_*mes 2 java null exception nullpointerexception
MovieList.java中main方法的运行时错误.
我不确定我的程序设计是否从根本上说非常好,但我想知道它崩溃的原因.提前致谢.
package javaPractical.week3;
import javax.swing.*;
public class Movie {
//private attributes
private String title;
private String movieURL;
private String year;
private String genre;
private String actor;
// constructor
Movie(String t, String u, String y, String g, String a) {
this.title = t;
this.movieURL = u;
this.year = y;
this.genre = g;
this.actor = a;
}
//getters and setters
public void setTitle(String t) {
this.title = t;
}
public String getTitle() {
return this.title;
}
public void set_url(String a) {
this.movieURL = a;
}
public String get_url() {
return this.movieURL;
}
public void setYear(String y) {
this.year = y;
}
public String getYear() {
return this.year;
}
public void setGenre(String g) {
this.genre = g;
}
public String getGenre() {
return this.genre;
}
public void setActor(String a) {
this.actor = a;
}
public String getActor() {
return this.actor;
}
//output movie details
public String toString() {
return ("Title: " + this.title + "\nURL: " + this.movieURL + "\nYear: "
+ this.year + "\nGenre: " + this.genre + "\nActor: "
+ this.actor);
}
public static void main(String[] args) {
//testing Movie class
Movie Movie1 = new Movie("Spiderman", "www.", "2002", "Action",
"Tobey M");
JOptionPane.showMessageDialog(null, Movie1.toString());
//testing MovieList class
}
}
Run Code Online (Sandbox Code Playgroud)
package javaPractical.week3;
import javax.swing.*;
import java.util.ArrayList;
public class MovieList1 {
private static ArrayList myFavouriteMovies = new ArrayList();
private static int NUM_OF_MOVIES = 10;
private int numberOfMovies = 0;
private int index = 0;
public MovieList1() {
this.myFavouriteMovies = null;
this.numberOfMovies = 0;
this.index = 0;
}
public int getNumberOfMovies() {
return this.myFavouriteMovies.size();
}
public boolean isEmpty() {
if (this.myFavouriteMovies.isEmpty()) {
return true;
} else
return false;
}
public static void main(String[] args) {
MovieList1 List = new MovieList1();
String titleADD;
String movieURLADD;
String yearADD;
String genreADD;
String actorADD;
titleADD = JOptionPane.showInputDialog(null, "Enter title:");
movieURLADD = JOptionPane.showInputDialog(null, "Enter URL:");
yearADD = JOptionPane.showInputDialog(null, "Enter year:");
genreADD = JOptionPane.showInputDialog(null, "Enter genre:");
actorADD = JOptionPane.showInputDialog(null, "Enter actor:");
Movie TempMovie = new Movie(titleADD, movieURLADD, yearADD, genreADD,
actorADD);
myFavouriteMovies.add(TempMovie);
}
}
Run Code Online (Sandbox Code Playgroud)
当它试图添加新程序崩溃Movie到myFavouriteMovies,因为myFavouriteMovies是null.
虽然myFavouriteMovies初始化为一个新的,空的ArrayList,然后null在MovieList1构造函数中设置为.
目前,myFavouriteMovies是static,因此每个MovieList1实例之间只共享此变量的一个副本.您可能希望static从myFavouriteMovies声明中删除修饰符.然后每个MovieList1对象都有自己的myFavouriteMovies字段.但是,您将在MovieList1该类中添加一个新方法,以允许您的main方法将影片添加到影片列表中,可能如下所示:
List.add(TempMovie);
Run Code Online (Sandbox Code Playgroud)
你还需要删除
this.myFavouriteMovies = null;
Run Code Online (Sandbox Code Playgroud)
从构造函数,因为已将其初始化为空ArrayList,您不希望将其设置为null.