aspose file tools*
The moose likes JDBC and the fly likes NPE for sql Connection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "NPE for sql Connection" Watch "NPE for sql Connection" New topic
Author

NPE for sql Connection

Padmanabh Sahasrabudhe
Ranch Hand

Joined: Mar 04, 2008
Posts: 53
This is making me mad
I have the following code for Connection:



I am getting NPE at second System.out.println(connection.toString()); statement and not at first. Not sure why?

I tried changing the driver to oracle.jdbc.OracleDriver but in vain!
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18907
    
    8

Let's suppose an exception is thrown before a value is assigned to the "connection" variable. You catch that exception and print a message. Then you carry on and try to use the "connection" variable, which is still null.

As you imply, the real problem is something else, namely something to do with loading the driver class. Your code printed the exception, which you didn't share with us. So read it and act accordingly.
Padmanabh Sahasrabudhe
Ranch Hand

Joined: Mar 04, 2008
Posts: 53
Thanks Paul. This is the exception I ma getting. It doesn't seem that problem with loading driver. I get the value of the drive printed in the output.

oracle.jdbc.driver.OracleDriver
java.lang.NullPointerException
at tests.functional.cartridge.spatial.common.apps.spatial_cartridge.SpatialDBHelper.getJDBCConnection(SpatialDBHelper.java:53)
at tests.functional.cartridge.spatial.common.apps.spatial_cartridge.SpatialDBHelper.main(SpatialDBHelper.java:24)

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18907
    
    8

Yes, I don't know why you would choose to print only that. Your next catch block prints the entire stack trace, which would have been far more useful.
Padmanabh Sahasrabudhe
Ranch Hand

Joined: Mar 04, 2008
Posts: 53
yes, you are right. it was a classpath issue for the driver and it was not loading. thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: NPE for sql Connection