FilmMain.main(FilmMain.java:50)处的线程“主”中的异常java.lang.NullPointerException

时间:2019-12-28 14:08:51

标签: java arraylist

我正在尝试存储我创建的类的对象。创建的对象具有Arraylist数据类型,但是当我尝试存储它时,它显示一个空的Pointer Exception。 问的问题是: “创建一个带有字符串对象的电影类,该对象存储名称,语言,lead_actor和类别(动作/戏剧/小说/喜剧)。还包括一个整数数据成员,用于存储电影的时长。包括参数化构造函数,默认构造函数和辅助函数可以使用构造函数或访问器函数初始化Flim对象;创建包含main函数的FilmMain类;在main函数中,创建一个矢量对象,将有关电影的信息存储为对象。向量类以迭代要显示的向量对象” 这是我的代码

import java.util.*;

class Film {
        ArrayList<String> name,language, lead_actor, category;
        int duration;

        Film(){
            name = null;
            language = null;
            lead_actor = null;
            category = null;
            duration = 0;
        }

        Film(ArrayList<String> n, ArrayList<String> lang, ArrayList<String> actor, ArrayList<String> cat, int dur){
            name = n;
            language = lang;
            lead_actor = actor;
            category = cat;
            duration = dur;
        }
}
public class FilmMain {
    public static void main(String[] args) {
        int i;
        Film films;
        Vector<Film> Films = new Vector<Film>();
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the number of films: ");
        int m = sc.nextInt();
        String a,b,c,d;

        for(i=1;i<m+1;i++){
            films = new Film();
            System.out.println("Enter Name for film " + i + ": ");
            a = sc.next();
            //System.out.println("ok");

            System.out.println("Enter Language for film " + i + ": ");
            b = sc.next();

            System.out.println("Enter Lead Actor for film " + i + ": ");
            c = sc.next();

            System.out.println("Enter Category for film " + i + ": ");
            d = sc.next();

            System.out.println("Enter Duration for film " + i + ": (in mins)");
            films.duration = sc.nextInt();
            films.name.set(i,a);
            films.language.set(i,b);
            films.lead_actor.set(i,c);
            films.category.set(i,d);
            Films.add(films);
        }
    }
}

0 个答案:

没有答案