简体   繁体   English

NullPointerException 当我使用参数调用构造函数时

[英]NullPointerException when i call a constructor with arguments

firstly, really sorry for my poor english.首先,真的很抱歉我的英语不好。 i am trying to make a list of movies.我正在尝试制作电影列表。 in main class i call the insert() method and in it i make an object of MovieListNode class in order to do what is needed.在主类中,我调用了 insert() 方法,并在其中创建了一个 MovieListNode 类的对象,以便执行所需的操作。

class main{...
   while( FileParsers.hasNextMovie() ){
        MovieData movie = FileParsers.getNextMovie();
        System.out.println( movie );
        /* fill the movie lists here */
        UnsortedMovieList vag=new UnsortedMovieList();
        vag.insert(movie);
}

the insert method of unsortedmovielist: unsortedmovielist的插入方法:

class UnsortedMovieList{...
 public void insert(MovieData data){

    MovieListNode node=new MovieListNode(data.getId(),data.getTitle(),data.getYear(),data.getRating(),data.getVotes(),data.getDuration(),data.getGenres());

    if(isEmpty()){
            tail=node;
    }else{
        head.setPrevious(node);
    }
    node.setNext(head);
    head=node;
    size++;

}

and the MovieListNode class(sorry for the size):和 MovieListNode 类(抱歉大小):

public class MovieListNode {

private int id;
private String title;
private int year;
private double rating;
private int votes;
private int duration;
private ArrayList<genre_t> genres;
private int i=0;

private MovieListNode previous;
private MovieListNode next;

public MovieListNode(){}

public MovieListNode(int id, String title, int year, double rating, int votes, int duration, ArrayList<genre_t> genres) {
    this.id=id;
    this.title=title;
    this.year=year;
    this.rating=rating;
    this.votes=votes;
    this.duration=duration;
    this.genres=genres;
}

public int getId() {return id;}
public String getTitle() {return title;}
public int getYear() {return year;}
public double getRating() {return rating;}
public int getVotes() {return votes;}
public int getDuration() {return duration;}
public ArrayList<genre_t> getGenres() {return genres;}
public MovieListNode getPrevious() {return previous;}
public MovieListNode getNext() {return next;}

public void setNext(MovieListNode next) {this.next=next;}
public void setPrevious(MovieListNode previous) {this.previous=previous;}

} }

when i do this i get NullPointerException in line MovieListNode node=new MovieListNode(data.getId(),data.getTitle(),data.getYear(),data.getRating(),data.getVotes(),data.getDuration(),data.getGenres()) .instead if i write 'MovielistNode node=new MovielistNode();'当我这样做时,我在MovieListNode node=new MovieListNode(data.getId(),data.getTitle(),data.getYear(),data.getRating(),data.getVotes(),data.getDuration(),data.getGenres())得到NullPointerException MovieListNode node=new MovieListNode(data.getId(),data.getTitle(),data.getYear(),data.getRating(),data.getVotes(),data.getDuration(),data.getGenres())如果我写'MovielistNode node=new MovielistNode();' i dont get any errors but it's not what i want.我没有收到任何错误,但这不是我想要的。 if anyone could help i would be grateful.如果有人可以提供帮助,我将不胜感激。 thanks.谢谢。 (if u want more information about something in my code please let me know) (如果您想了解有关我代码中某些内容的更多信息,请告诉我)

One or all of the fields in your MovieData object are null. MovieData对象中的一个或所有字段为空。 You need to investigate your method:您需要调查您的方法:

FileParsers.getNextMovie();

You use this method to initialize an object of type MovieData If this method does not declare and initialize a MoveData object with a constructor that initializes all of the data fields, and then return that object, you will get a NullPointer when you try to call one of the getters您使用此方法初始化MovieData类型的对象 如果此方法未使用初始化所有数据字段的构造函数声明和初始化MoveData对象,然后返回该对象,则当您尝试调用一个对象时,您将得到一个NullPointer getters

It is probably genres since the other fields have default initializations.它可能是genres因为其他字段具有默认初始化。

UPDATE:更新:

In your Fileparsers class.在您的Fileparsers类中。 I notice the code in the getNextMovie() method:我注意到getNextMovie()方法中的代码:

if( dataLine==null || genresLine==null ) 
  return null; 
}

It may be that your logic to readLine() is not being used correctly.可能是您的readLine()逻辑未正确使用。 So I suspect you may be ending up with null lines and therefore returning a null MovieData object.所以我怀疑你可能会以空行结束,因此返回一个空的MovieData对象。 You should check if the next line is null before assigning it.您应该在分配之前检查下一行是否为空。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 访问构造函数时发生NullPointerException - NullPointerException when accessing constructor 调用自定义活动子类时出现NullPointerException - NullPointerException when I call a custom activity subclass 在Java中,评估构造函数调用的参数引发异常时会发生什么? - In Java, what happens when evaluating the arguments of a constructor call throws an exception? 调用构造函数抛出“java.lang.NullPointerException” - Call constructor throws 'java.lang.NullPointerException' 当我在构造函数中调用它时,自动装配的属性是 null - Autowired property is null when I call it in constructor 当我在 Activity 中调用构造函数时应用程序崩溃 - App crashes when I call a constructor in an Activity 当我尝试调用超级构造函数时,复制构造函数生成错误 - Copy constructor is generating an error when I try to call the super constructor 没有传递任何参数时,我应该在构造函数中使用关键字“ this”吗? - Should I be using keyword “this” in a constructor when no arguments are being passed? 当我从android服务调用类时出现NullPointerException - NullPointerException when i call a class from an android service Java NullPointerException当我在自定义类上调用方法时 - Java NullPointerException When I call a method on a custom class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM