0

Having some problems trying to setup a DB connection from a servlet.

I have the following class that handles the DB connection in the servlet but I keep getting an NullPointerException for Class.forName().

DB Util Class:

public class Database {

    private static Connection connection = null;

    /**
     * 
     * @return
     */
    public static Connection getConnection(){
        if(connection!=null){
            return connection;
        } else {
            try {
                Properties db_properties = new Properties();
                InputStream inStream = Database.class.getClassLoader().getResourceAsStream("/db.properties");
                db_properties.load(inStream);
                //String dbDriver = db_properties.getProperty("driver").trim();
                String url = db_properties.getProperty("url").trim();
                String dbUser = db_properties.getProperty("user").trim();
                String dbPass = db_properties.getProperty("pass").trim();
                Class.forName("com.mysql.jdbc.Driver");
                connection = DriverManager.getConnection(url,dbUser,dbPass);
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return connection;
    }
}

With the following properties file (I tried hardcoding the driver in and still get the same error!):

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:8889/gymbuddy
user=Mathan
pass=PYCCmcPDAFSj7N9B

And I get the following Stack trace:

SEVERE: Allocate exception for servlet BuddyController
java.lang.NullPointerException
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:171)
    at com.mathanv.gb.util.Database.getConnection(Database.java:35)
    at com.mathanv.gb.dao.BuddyDao.<init>(BuddyDao.java:35)
    at com.mathanv.gb.controller.BuddyController.<init>(BuddyController.java:29)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at java.lang.Class.newInstance0(Class.java:357)
    at java.lang.Class.newInstance(Class.java:310)
    at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:138)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1137)
    at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:858)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:136)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
    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:1004)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
    at java.lang.Thread.run(Thread.java:680)

I'm trying to send a post request from a Android client. The HTTPpost request seems to work! But getting the connection gives me the above exception! Help!

The following is my Servlet layout:

Servlet Layout

Let me know if you need any more information!

MathanMV
  • 412
  • 1
  • 5
  • 16

4 Answers4

1

The only way you can get that exception is by passing null to Class.forName() or maybe by having a corrupt JVM. The code you've shown can't throw that exception. Double check your results. At worst, you should get a ClassNotFoundException. Maybe you've updated your code but haven't compiled and redeployed it yet, so you're seeing the results of old code running in the server still.

Ryan Stewart
  • 126,015
  • 21
  • 180
  • 199
  • 1
    The problem was the server not updating my changes. Had to make sure by cleaning project and terminating the server and restarting as well as restarting the android emulator. Thanks a lot mate! – MathanMV Mar 05 '13 at 00:31
  • Yeah, in my case, i need to do a maven project update too, somewhere some configuration got borked – chrismarx Apr 17 '15 at 18:44
0

Are you sure when you hard coded the driver name in the code then also you are getting same error. Class.forName() throws null pointer exception when you pass null as parameter. i think in the first case property file is not loaded and driver name returns null.

Abhishek Kumar
  • 229
  • 1
  • 5
-1

This happens if you have a mysql driver in more locations.

Try to look for other locations.Or remove i from the lib folder. If it is as I say the problem should be solved.

Gyonder
  • 3,674
  • 7
  • 32
  • 49
  • Having the driver jar in multiple places won't give you a NPE. You just might not get the driver version you think you're getting. – Ryan Stewart Mar 03 '13 at 13:18
  • Sorry, but that won't happen either with multiple driver jars. That would be if the driver is missing from the class path entirely. – Ryan Stewart Mar 03 '13 at 13:57
  • Ok @Ryan Stewart that is what I would say as well but It seems that someone is saying the contrary. http://stackoverflow.com/questions/1585811/classnotfoundexception-com-mysql-jdbc-driver – Gyonder Mar 03 '13 at 14:05
  • The accepted answer there is talking about having two class paths and mistakenly using the wrong one, not two copies of the jar on the same class path. – Ryan Stewart Mar 03 '13 at 14:10
-1

Delete your code immediately and use a DataSource right now in Tomcat. Your code is far away from being high quality.

Michael-O
  • 18,123
  • 6
  • 55
  • 121