Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java null pointer exceptions - don't understand why

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);   
    }
}
like image 821
James Avatar asked Sep 05 '26 05:09

James


2 Answers

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.

like image 56
Richard Fearn Avatar answered Sep 06 '26 21:09

Richard Fearn


Within your constructor you are setting

 public MovieList1() {
   this.myFavouriteMovies = null;
   this.numberOfMovies = 0;
   this.index = 0;
 }

after you already declared myFavouriteMovies above. This could result in a NullPointer

like image 42
keyboardsurfer Avatar answered Sep 06 '26 19:09

keyboardsurfer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!