Win a copy of Java 9 Revealed this week in the Features new in Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

stmt = conn.createStatment(); Null pointer  RSS feed

 
Jason Lalumiere
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please help.

stmt = conn.createStatement(); is giving me a Null pointer here is the stack trace

18-Nov-2014 12:44:14.762 SEVERE [http-apr-8085-exec-221] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [SQLServlet2] in context with path [/SQLAssignment] threw exception
java.lang.NullPointerException
at sql.JBDC.exactQueryFromUserInput(JBDC.java:49)
at sql.SQLServlet2.processRequest(SQLServlet2.java:68)
at sql.SQLServlet2.doGet(SQLServlet2.java:106)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:618)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:534)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1081)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:658)
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:277)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2381)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2370)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)


My code
JDBC

Servlet


I have ran this through a tester class, with a main method and hard coding in the value in conn.exactQueryFromUseInput("Toronto");
will return Toronto + the population...

Whenever I transfer this code, I get stuck with the following above. Some advice would be greatly appreciated

SQLServlet2 was created in a different project. It has since been deleted and the code recycled into this. I don't know why it's saying for[SQLServlet2] and [/SQLAssignment]
 
Dave Tolls
Ranch Hand
Posts: 2554
27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do your logs show?
Do they show that the connect() call has succeeded?
Because it looks to me like it hasn't, if you're getting an NPE.
 
khadar valli
Greenhorn
Posts: 26
Java MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
first load the JDBC driver after that create connection...

like


Class.forName("DRIVER NAME WHATEVER YOU ARE USING");
 
Dave Tolls
Ranch Hand
Posts: 2554
27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
khadar valli wrote:first load the JDBC driver after that create connection...

like


Class.forName("DRIVER NAME WHATEVER YOU ARE USING");


Depends on the driver.
A JDBC 4.0 driver does not require that.

ETA: In any case there would be an exception thrown.
 
Paul Clapham
Sheriff
Posts: 22199
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There probably IS an exception being thrown; since the "conn" variable is null, that means that the code which loads the database driver and creates the connection is failing. But unfortunately the exception is being written to System.err, which is a bad idea in a web application. The OP would have to find out where his web application server writes System.err output to and look there to see what's happening.

Notice also that the JDBC.connect() method basically ignores the exception (except for noting it in System.err) and continues as if nothing bad had happened. This is also a bad idea; it should just allow the exception to be thrown to its caller instead.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!