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

NullPointerException

 
hesham katon
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Everyone,
I have a class which working good at a server , then when i moved it to another server it generates NullPointerException at this line :
statement=con.createStatement();
it load the driver well , and create the connection , then the problem happend while creatStatement .
Do you have any Idea about that
Thanks alot
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34384
346
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure that it creates the connection ok? From your post, it looks like con is null. That would explain the null pointer exception.
 
Nagendra Prasad
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
as mentioned in the previous post, connection could be null.
you could use some defensive coding here...
if (conn != null)
{
// process further
}else
{
// handle error gracefully.
}
while moving servers or working in such an env, (we have a similar one.. where we have to connect to more than one data source), it is useful to know agains which datasource connections are being attempted and with what parameters.
This could be included in the exception being thrown!
 
hesham katon
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks alot .
You are right , connection was null .
so the problem now with :
connection = DriverManager.getConnection("jdbc:mysql://[hostname]/[dbname]?user=[username]&password=[password]");
it generates sql exception
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic