• Post Reply Bookmark Topic Watch Topic
  • New Topic

createStatement NPE with JSP

 
Mike Stanley
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Ranchers,

I have tried searching around for the answer to this for hours but could not find a solution so hopefully someone can help. I have created an application which sends values to a Data Access Object from a test class. I used a DBConnectionFactory to setup up the connections etc. That all worked perfectly but it was until I had to use it with a jsp is the problem.
I copied the code from my test class which worked and used it in the jsp like so:


The webpage appears, i enter in the details and submit them and then i get the following error message. (Using Tomcat server)

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: An exception occurred processing JSP page /adduser.jsp at line 201

198: usr.setCountry(country);
199: usr.setType(type);
200:
201: usrDAO.insert(usr);
202:
203: }
204:


Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:505)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:416)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:390)


root cause

java.lang.NullPointerException
investmentclub.UserDAO.insert(UserDAO.java:126)
org.apache.jsp.adduser_jsp._jspService(adduser_jsp.java:255)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:390)



So I check the UserDAO class and it says the null pointer exception is at the Statement st = conn.createStatement(); line. (Below)



Like I have said I tried searching for similar problems for hours on the net and the only solutions i found were that people made spelling mistakes which isn't my problem. I apologise if it is something trivial or obvious but sometimes you need the obvious pointed out!
Hope to hear from someone soon,

Mike.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65524
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you originally wrotre the code outside of a JSP and then moved it into a JSP? Why?

Java code should never be placed in a JSP - that's an artifact from the dawn of JSP. Put the code back into Java classes (where it's also easier to debug) and use JSP only to render the HTML views.

This article might help with understanding modern web application structure best practices.

With regards to NPE's: the stack trace shows you the line on which the null reference occurred. Fromt hat you need to backtrack t discover why the reference that should not be null is null.

 
Mike Stanley
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:So you originally wrotre the code outside of a JSP and then moved it into a JSP? Why?

Java code should never be placed in a JSP - that's an artifact from the dawn of JSP. Put the code back into Java classes (where it's also easier to debug) and use JSP only to render the HTML views.

This article might help with understanding modern web application structure best practices.

With regards to NPE's: the stack trace shows you the line on which the null reference occurred. Fromt hat you need to backtrack t discover why the reference that should not be null is null.


Thanks for replying.

It was originally a stand alone application which i am now integrating with jsp. I understand it is bad practice to place a lot of java code in a jsp but as i have only been at it a short time i feel i should be allowed the ignorance to "Wing It" as you say in your article.
But surely if the code works in a java class it would work in the jsp? I thought that the NPE would have been because the values being passed were null but i have tested them and they are right. But it is just the fact it is pointing to the createStatement... It didn't give an npe when the code in the jsp was in a java class so i just find it confusing that it is doing it now.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!