Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Possible Duplicate:
Java null pointer exceptions - don't understand why…

MOVIE.JAVA

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

}
}

MOVIELIST.JAVA

package javaPractical.week3;

import javax.swing.*;
import java.util.ArrayList;

public class MovieList1 {

private static ArrayList<Movie> 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);

    // crashes here
    myFavouriteMovies.add(TempMovie);

}
}
share|improve this question
1  
And your question is? See tinyurl.com/so-hints –  Jon Skeet Oct 31 '10 at 16:02
    
Do you have a stacktrace available? It will be beneficial to post the stacktrace so we can tell you how to read them so you would be able to correct this problem if it arises again. –  Anthony Forloney Oct 31 '10 at 16:02
    
@user492837 : Add StackTrace, plz. –  Stas Kurilin Oct 31 '10 at 16:02
add comment

marked as duplicate by nc3b, BalusC, duffymo, meriton, Charles Duffy Oct 31 '10 at 20:27

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2 Answers

You have defined static attribute private static ArrayList<Movie> myFavouriteMovies = new ArrayList();

But in the constructor you are assigning the null. After that you are invoking calls like myFavouriteMovies.size() which causes NullPointerException

public MovieList1() {
    this.myFavouriteMovies = null;
    this.numberOfMovies = 0;
    this.index = 0;
}
share|improve this answer
    
+1, Good catch, you had better eyes than me. –  Anthony Forloney Oct 31 '10 at 16:08
add comment

Of course it crashes - you've set it to null.

Why on earth didn't you heed the perfectly good advice you received here?

http://stackoverflow.com/questions/4061075/java-null-pointer-exceptions-dont-understand-why

You're wasting everyone's time on a trivial question. I'm voting to close.

Try this - it's still heinous, but it runs:

    package javaPractical.week3;
import javax.swing.*;
import java.util.ArrayList;
import java.util.List;

public class MovieList1
{

    private static int NUM_OF_MOVIES = 10;

    private List<Movie> myFavouriteMovies;
    private int numberOfMovies = 0;
    private int index = 0;

    public MovieList1()
    {
        this.myFavouriteMovies = new ArrayList<Movie>();
        this.numberOfMovies = 0;
        this.index = 0;
    }

    public int getNumberOfMovies()
    {
        return this.myFavouriteMovies.size();
    }

    public boolean isEmpty()
    {
        return this.myFavouriteMovies.isEmpty();
    }

    public void add(Movie movie)
    {
        this.myFavouriteMovies.add(movie);
    }

    @Override
    public String toString()
    {
        return "MovieList1{" +
               "myFavouriteMovies=" + myFavouriteMovies +
               '}';
    }

    public static void main(String[] args)
    {
        MovieList1 movieList = 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);

        // crashes here
        movieList.add(TempMovie);
        System.out.println(movieList);

    }
}

class Movie
{
    private String title;
    private String url;
    private String year;
    private String genre;
    private String actor;

    Movie(String title, String url, String year, String genre, String actor)
    {
        this.title = title;
        this.url = url;
        this.year = year;
        this.genre = genre;
        this.actor = actor;
    }

    @Override
    public String toString()
    {
        return "Movie{" +
               "title='" + title + '\'' +
               ", url='" + url + '\'' +
               ", year='" + year + '\'' +
               ", genre='" + genre + '\'' +
               ", actor='" + actor + '\'' +
               '}';
    }
}
share|improve this answer
    
Thank you very much duffymo (and others), got it fixed AND I understand why as well. –  user492837 Oct 31 '10 at 16:15
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.