0

I have a button which just read all fields from database tables (select * from movies), so I use AbstractTableModel. First table read OK with this class, but if I implement AbstractTableModel for other table I got NPE.

Here's my code TableModelForMovies:

package ua.movies.movie;

import java.util.ArrayList;
import java.util.List;

import javax.swing.table.AbstractTableModel;

public class TableModelForMovies extends AbstractTableModel {

    private static final int NAME_COL = 0;
    private static final int Genre_COL = 1;
    private static final int Release_Date = 2;
    private static final int Unique = 3;

    private String[] columnNames = { "name", "genre", 
    "release" , "dgdggdf" };

    private List<Movie> movies;

     public TableModelForMovies(List<Movie> theMovie) {
        movies = theMovie;
    }

    @Override
    public int getRowCount() {
        return movies.size(); <<- here is problem
    }

    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public String getColumnName(int col) {
        return columnNames[col];
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        Movie tmpMovie = movies.get(rowIndex);
        switch (columnIndex) {
        case NAME_COL:
            return tmpMovie.getMovieName();
        case Genre_COL:
            return tmpMovie.getGenre();     
        case Release_Date: 
            return tmpMovie.getRelease();
        case Unique: 
            return tmpMovie.getUniqueKey();

        default:
            return tmpMovie.getMovieName();
        }
    }
}

and listener on my button

try {
    String movieName  = textFindField.getText();
    List<Movie> employees = null;
    if (movieName != null && movieName.trim().length() > 0) {
        //employees = movieDao.search(lastName);
        employees = movieDao.getAllMovies();
    } else {
        searchByName();
    }

    // create the model and update the "table"
    TableModelForMovies model = new TableModelForMovies(employees);

    tableMovies.setModel(model);
} catch (Exception exc) {
    JOptionPane.showMessageDialog(AppMovies.this, "Error: " + exc, "Error", JOptionPane.ERROR_MESSAGE); 
}

I really don't understand why I got NPE. Because I have second class and it also extends AbstractTableModel and there aren't any errors , NPE just here.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
M-Misha-M
  • 33
  • 9
  • 1
    What happens with the employees object if there is no movie name? – paisanco Jun 01 '16 at 00:29
  • @paisanco , yea i forgot to call a method , it will output all our table, in another case we can find by name , but NPE in class TableModel – M-Misha-M Jun 01 '16 at 00:38
  • 1) A single blank line of white space in source code is all that is *ever* needed. Blank lines after `{` or before `}` are also typically redundant. 2) Words typed in all lower case are hard to read, like trying to listen to someone who is mumbling. Please use an upper case letter at the start of sentences, for the word I, and proper names like `ArrayList` or Oracle. 3) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). Hard-code some data to replace the DB. – Andrew Thompson Jun 01 '16 at 01:53

1 Answers1

0

It may possible that, getRowCount() method get called before the movies list is initialized, when you are overriding getRowCount() method, please use following pattern:

 @Override
  public int getRowCount()
  {
    int result = 0;
    synchronized(LOCK) {
      if(movies != null) {
        result = movies.size();
      } // if
    } // synchronized
    return result;
  }
Sandeep Kokate
  • 825
  • 4
  • 16
  • ok , thank you very much , i don't use your methid , i did the mistake in my class with getter and setters , now it work but thanks) – M-Misha-M Jun 01 '16 at 13:54