Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

database doesn't fill List

 
natasa jones
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all!
i am trying to fill a List with values from a table..but i am having a hard time to do so!the problem is that it doesnt store any values in List and it returns java.lang.NullPointerException.my code is the following:

private static final String SQL_Movie_Categories = "SELECT MOVIE_CATEGORY_ID, DESCRIPTION from movie_category";

public List getMovieCategories() throws ServiceException {
MovieCategory movieCategory;
List movieCategoryList = null;
PreparedStatement preparedStatement = null;
Connection connection = null;
ResultSet resultSet = null;
try {

connection = dataSource.getConnection();
preparedStatement = connection.prepareStatement(SQL_Movie_Categories);
resultSet = preparedStatement.executeQuery();
try {
while (resultSet.next()) {

movieCategory = new MovieCategory();
movieCategory.setId(resultSet.getString(1));
movieCategory.setDescription(resultSet.getString(2));
System.out.println("id=" + movieCategory.getId());
System.out.println("the item fetched is:" + movieCategory.getDescription());
movieCategoryList.add(movieCategory);

}
} catch (java.lang.NullPointerException exc) {
System.out.println("null pointer exception");
}
return movieCategoryList;
} catch (SQLException ex) { // Close connection,resultSet,prepareStatements etc...
where MovieCategory is a BO i have made,which has movie_description and movie_id Strings and get and set methods.

can anyone help???it prints the data of the first database record but then..it doesnt store it and has a NullPointerException.
thanks in advance!
 
natasa jones
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well i finally found it!
i should have declared my list List MovieCategoryList=new ArrayList(). ;)
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34837
369
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Natasa,
Thanks for posting the solution so that others who see this thread in the future can benefit.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic