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.