Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) 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
Sheriff
Posts: 35976
422
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
 
Happiness is not a goal ... it's a by-product of a life well lived - Eleanor Roosevelt. Tiny ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!