1
votes

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!

NOTE:

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

Libraries currently in my netbeans project


The BugValidator jar contains the mysql-connector jar which has the Drivers class

Contents of the BugValidator jar


Contents of mysql-connector-java-5.1.7-bin.jar

4
Have you included the mySQL JDBC driver in your classpath? - Alex
The validator class is an external jar and it contains another jar that has the mySQL JDBC driver class in the com.mysql.jdbc folder - Shahab
Nested jar files are not included in your classpath. So if you are including BugValidator.jar, the jars inside of it will not be in the classpath. Make sure that on the server the JDBC driver jar is explicitly in the classpath - Alex
The BugValidator contain a file called ".classpath" and has this in it: <classpathentry kind="lib" path="lib/mysql-connector-java-5.1.7-bin.jar"/>, would that be an explicit declaration - Shahab
You need to extract mysql-connector-java-5.1.7-bin.jar and place it the lib directory alongside BugValidator.jar - Alex

4 Answers

5
votes

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!

2
votes

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?

1
votes

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

0
votes

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.