File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes database doesn't fill List Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "database doesn Watch "database doesn New topic
Author

database doesn't fill List

natasa jones
Ranch Hand

Joined: Aug 20, 2006
Posts: 45
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

Joined: Aug 20, 2006
Posts: 45
Well i finally found it!
i should have declared my list List MovieCategoryList=new ArrayList(). ;)
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29257
    
140

Natasa,
Thanks for posting the solution so that others who see this thread in the future can benefit.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
 
wood burning stoves
 
subject: database doesn't fill List
 
Similar Threads
Value in dropdown from servlet
How to use httpsession with dao factory
What is -Xlint in compiling java program?why this is done?
Another "com.mysql.jdbc.Driver" Connection problem
resultset value in vector