0

I have a database with movies and I have movie as a class. I am tring to get the movies from the database loaded into my GUI, but i cannot seem to get them into my arraylist. I get a null exception and I don't see where I'm going wrong.

public ArrayList getMovies(ArrayList movieList2)
{
    movieList = movieList2;
    Connection con = null;
    try {
        con = DriverManager.getConnection(
                databaseURL, user, password);
        Statement sta = con.createStatement();


        ResultSet res = sta.executeQuery(
                "SELECT * FROM MOVIES");
        System.out.println("List of Movies: ");
        while (res.next()) {
            Movie movie = new Movie();
            movie.setTitle(res.getString((String)"Title"));
            movie.setYear(res.getInt("Yearmade"));
            movie.setGenre(res.getString("Genre"));
            movie.setDuration(res.getInt("Duration"));
            movie.setActors(res.getString("Actor"));
            movie.setDirector(res.getString("Director"));
            movieList2.add(movie);
        System.out.println(movie);
        }
        res.close();
        sta.close();
        con.close();
    } catch (Exception e) {
        System.err.println("Exception: " + e.getMessage());
    }
    System.out.println(movieList2);
    return movieList2;
}

This is output from the stacktrace run:

List of Movies: 
Exception: null
null
BUILD SUCCESSFUL (total time: 0 seconds)

public static void main(String[] args) {
    controller.getMovies(movieList);
}
JonanK
  • 45
  • 1
  • 7
  • post the stack trace – Shriram Feb 03 '16 at 09:27
  • Please include the error stack in your question. – user2004685 Feb 03 '16 at 09:27
  • run: List of Movies: Exception: null null BUILD SUCCESSFUL (total time: 0 seconds) – JonanK Feb 03 '16 at 09:30
  • is movieList2 passed as null? – Akshay Gehi Feb 03 '16 at 09:32
  • what is `movieList ` here `movieList = movieList2` – Braj Feb 03 '16 at 09:33
  • 1
    Have you initialized movieList2 ? what is the type of movieList ? Post the caller & stack trace. Try initiializing, movieList2 = new new ArrayList(); – Mani Feb 03 '16 at 09:34
  • Replace `System.err.println("Exception: " + e.getMessage()); ` by `e.printStackTrace();` it will show on what line it crashes (well everyone has found it's on `movieList2.add(movie);` because movieList2 == null). – StephaneM Feb 03 '16 at 09:36
  • movieList is an arraylist with movies, defined in the top of the class – JonanK Feb 03 '16 at 09:36
  • List of Movies: java.lang.NullPointerException null at Controller.Controller.getMovies(Controller.java:80) at Controller.Controller.main(Controller.java:27) BUILD SUCCESSFUL (total time: 0 seconds) I now know why i get null point. i run the method with the empty arraylist movieList, but how do i get it to run with the list made in the method? – JonanK Feb 03 '16 at 09:39
  • First debug it stand alone by changing caller as public static void main(String[] args) { movieList = new ArrayList(); controller.getMovies(movieList); } and Removing below line in getMovies method movieList = movieList2; – Mani Feb 03 '16 at 09:46
  • @user3498874 that's it, i got it working now. if you make an answer i will accept it! – JonanK Feb 03 '16 at 10:00
  • Given it in answer section now. – Mani Feb 03 '16 at 10:27

1 Answers1

1
Initialize movieList in caller as below,
public static void main(String[] args) {
 movieList = new ArrayList(); 
controller.getMovies(movieList);
 }  

and Removing below line in getMovies method 
 movieList = movieList2;
Mani
  • 259
  • 3
  • 6