简体   繁体   中英

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.

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:

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):

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();' 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. 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

It is probably genres since the other fields have default initializations.

UPDATE:

In your Fileparsers class. I notice the code in the getNextMovie() method:

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

It may be that your logic to readLine() is not being used correctly. So I suspect you may be ending up with null lines and therefore returning a null MovieData object. You should check if the next line is null before assigning it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM