Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help - Statement error!

 
Lucky Narayan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi JDBC gurus,
I am facing a very wierd problem - any help appreciated.
Here is the simple code:
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection c = DriverManager.getConnection(connURL, dbUserName, dbPassword);
System.out.println("Connected correctly: ");
}
catch(ClassNotFoundException cnfe)
{
System.out.println("Error loading driver: " + cnfe);
}
The above works fine but when I add this line:
Statement stmt = c.createStatement();
it fails, giving the following error:
java.lang.NullPointerException
at myClasses.DbBean.isLoginValid(DbBean.java:107)
pointing to line which has the Statemnt stmt =...
Any ideas why? I am going crazy here
Thanks!
 
raghavender rao
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi ther,
i think Connection object is null...
try
System.out.println(con);
 
Jamie Robertson
Ranch Hand
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
by the time you use your connection it is out of scope! Thus its reference is null. To get around this, declare your connection outside the try block:

Jamie
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic