Reputation: 2067
I am trying to run a web application that gets a specific length of numbers from a user and checks that against a database (so if the user made a typo, it will makes suggestions as to what key the user actually meant). The problem I am running into is the application won't connect to the database, I get this error:
java.lang.NullPointerException
at Validator.Validate.checkIDs(Validate.java:410)
at Validator.Validate.getAllValidIds(Validate.java:203)
at Python.WebServlet.processRequest(WebServlet.java:70)
at Python.WebServlet.doPost(WebServlet.java:247)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:987)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:579)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1805)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)
Line 410 in Validator.Validate is:
Statement stmt = Conn.createStatement();
Where Conn is a java.sql.Connection variable
Line 203 is a call to the method that does the ^ statement
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1701)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1546)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at Validator.Validate.dbConnect(Validate.java:394)
at Validator.Validate.getAllValidIds(Validate.java:193)
at Python.WebServlet.processRequest(WebServlet.java:70)
at Python.WebServlet.doPost(WebServlet.java:247)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:987)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:579)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1805)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)
This is the error I get on my server, if I run it on my local machine it works just fine (it validates the ID). What could be the problem here? Cannot it not locate the com.mysql.jdbc.Driver class? Is that class the issue?
Thanks for the advice/help in advance!
The Validator class is an external jar that is in my class path. It calls on another jar in its class path the contains the com.mysql.jdbc classes, as you can see in the following pictures
The BugValidator jar contains the mysql-connector jar which has the Drivers class
Upvotes: 1
Views: 13564
Reputation: 7769
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
You'll have to include the MySQL JDBC-Driver in your server's classpath.
Either by publishing it with your application or putting it in your server's lib
directory.
Hope this helps. Have Fun!
Upvotes: 5
Reputation: 1108632
The JDBC driver needs to go in the webapp's runtime classpath, not in some arbitrary /lib
folder, let alone embedded in a different JAR file (that would also not work in normal Java applications by the way).
Put the libraries which the webapp requires during runtime individually in webapp's /WEB-INF/lib
folder.
Unrelated to the concrete problem, please use package names according Java naming conventions.
Upvotes: 0
Reputation: 1257
Did you load the driver class??
Class.forName("com.mysql.jdbc.Driver");
i.e.:
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/karthicraj","mysql","mysql");
stm=con.createStatement();
String strQry="update sriram set age=?,salary=? where name=? and age=?";
Extracted from: http://forums.mysql.com/read.php?60,42473,275468#msg-275468
Upvotes: 1
Reputation: 3602
From this:
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
It's pretty clear that you don't have the MySQL driver on your ClassPath. Somewhere in your project, you should have the jar that contains the MySQL driver. If you don't, then you either need to download it to your lib directory, or set up a Maven dependency to it, depending on how your projects dependencies are being handled.
EDIT: In response to your update. All your screenshot shows is that everything is set up fine for compilation within your IDE. But we still don't know where you are running your application from that's giving you this stack trace.
Are you running your app from inside your IDE and getting the stack trace from the console? Are you running it after it's been packaged and seeing the stack trace in some logs?
Upvotes: 2