Hi, I'm running into a problem. I have a servlet that queries a database and then takes that resultSet and adds it as an attribute to the request and then the servlet forwards this to a jsp page. Inside the jsp I try to extract the data from the resultset, but I keep getting a NullPointerException. I know the getQuery method to get a database query works fine. I suspect there is a problem with me setting the attribute. I was told that I could set anything as an attribute. Here's the relevant code from the servlet:
and here's the relevant code from the jsp:
I keep getting a NullPointerException from the scriptlet above. Am I doing something wrong? Thanks for any help.
Sean, You can't access a resultset object after the statement or connection is closed. I would think this would throw a SQLException, but I guess it could throw a null pointer. Try copying the data into a java object (like ArrayList) and accessing it that way.
As already posted by Jeanne, your resultset probably does not exist at this point. it's not a good idea to pass resultsets around anyway. To verify that you can do... Object results = (Object)request.getAttribute("key1"); Then check if results is null. Just use a collection to store your values and then pass that around. You can also encapsulate your results in a class and then store that in the collection.
Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
As stated above. I would suggest you wrap your playerID in a Transfer Object or Value Object, a collection is good also. For example, I would build the TO or VO in your getQuery(sql) method and pass it back to the calling program. Craig
Joined: Dec 16, 2000
Thanks for all the helpful replies. The main culprit to my problem was that I didn't have the mysql driver in the tomcat/common/ directory. As soon as I put it there, and then used an ArrayList everything worked beautifully. Thanks for all the help.
Originally posted by Sean Casey: The main culprit to my problem was that I didn't have the mysql driver in the tomcat/common/ directory. As soon as I put it there, and then used an ArrayList everything worked beautifully.
Just to clarify.... The main culprit was the fact that you were trying to pass a ResultSet back to your JSP page and access it there. As was stated, a ResultSet it tied to a connection and without that connection, your ResultSet has no data. The reason you fixed your problem really has nothing to do with the Driver (unless there were other issues you left out). The fix was putting the data in an ArrayList and passing that to your JSP. I just didn't want you to be mislead on your solution. [ January 25, 2004: Message edited by: Gregg Bolinger ]