Java空指针异常 - 不明白为什么 [英] Java null pointer exceptions - don't understand why

查看:171
本文介绍了Java空指针异常 - 不明白为什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我不知道我的程序设计是否从根本上非常好,但我想知道为什么它崩溃了感谢提前。

  package javaPractical.week3; 

import javax.swing。*;

public class Movie {
// private attributes
private String title;
private String movieURL;
private String year;
私人字符串类型;
private String actor;

//构造函数
Movie(String t,String u,String y,String g,String a){
this.title = t;
this.movi​​eURL = u;
this.year = y;
this.genre = g;
this.actor = a;

}
// getter和setter
public void setTitle(String t){
this.title = t;
}

public String getTitle(){
return this.title;
}

public void set_url(String a){
this.movi​​eURL = a;
}

public String get_url(){
return this.movi​​eURL;
}

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;
}


//输出电影细节
public String toString(){
return(Title:+ this.title +\ nURL:+ this.movi​​eURL +\\\
Year:
+ this.year +\\\
Genre:+ this.genre +\\\
Actor:
+ this.actor);
}

public static void main(String [] args){
// testing Movie class
Movie Movie1 = new Movie(Spiderman,www。 ,2002,行动,
Tobey M);

JOptionPane.showMessageDialog(null,Movie1.toString());
//测试MovieList类
}
}






  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,输入标题:);
movieURLADD = JOptionPane.showInputDialog(null,Enter URL:);
yearADD = JOptionPane.showInputDialog(null,输入年:);
genreADD = JOptionPane.showInputDialog(null,Enter genre:);
actorADD = JOptionPane.showInputDialog(null,Enter actor:);

Movie TempMovie = new Movie(titleADD,movieURLADD,yearADD,genreADD,
actorADD);

myFavouriteMovies.add(TempMovie);
}
}


解决方案

由于 myFavouriteMovies Movie 添加到 myFavouriteMovies c $ c>是 null



尽管 myFavouriteMovies 被初始化为一个新的空的 ArrayList ,然后在 MovieList1中设置为 null / code>构造函数。



目前, myFavouriteMovies static ,所以每个 MovieList1 实例之间共享一个这个变量的副本。您可能要从 myFavouriteMovies 声明中删除 static 修饰符。然后每个 MovieList1 对象将有自己的 myFavouriteMovies 字段。然而,您可以向 MovieList1 类添加一个新方法,以允许您的方法将电影添加到电影列表,也许是这样的:

  List.add(TempMovie); 

此外,您需要删除

  this.myFavouriteMovies = null; 

从构造函数中,因为已将其初始化为一个空的 ArrayList ,您不想将其设置回 null


Run time error on main method in MovieList.java.

I'm not sure my program design is fundamentally very good, but I'd like to know why it crashes. Thanks in advance.

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
    }
}


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);   
    }
}

解决方案

The program crashes when it tries to add the new Movie to myFavouriteMovies, because myFavouriteMovies is null.

Although myFavouriteMovies is initialised to a new, empty ArrayList, it's then set to null in the MovieList1 constructor.

At the moment, myFavouriteMovies is static, so there's only one copy of this variable shared between every MovieList1 instance. You probably want to remove the static modifier from the myFavouriteMovies declaration. Then each MovieList1 object will have its own myFavouriteMovies field. However you'll then to add a new method to the MovieList1 class to allow your main method to add the movie to the movie list, perhaps like this:

List.add(TempMovie);

Also you'll need to remove

this.myFavouriteMovies = null;

from the constructor, because having initialised it to an empty ArrayList, you don't want to set it back to null.

这篇关于Java空指针异常 - 不明白为什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆