Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Basic Java question regarding objects initialization 1

Status
Not open for further replies.

thelordoftherings

Programmer
May 16, 2004
616
IL
Hello,

When I am writing this code:

Connection connection;

try
{

connection = DriverManager.getConnection...
}
catch( SQLException e )
{
e.printStackTrace();
}
finally
{
try
{
connection.close();
}
catch( SQLException e )
{
e.printStackTrace();
}
}


I receive a complitation error on this line: connection.close(): "Connection may have not have been initialized". But of course it may not have been initialized, this is why the try...catch are for. So why am I receiving this error?

Roy
 
Because is the call to DriverManager.getConnection() fails - then "connection" object will not have been initialized - which would cause an error in the finally block.

Do :

Connection connection = null;

You should also say :

if (connection != null) connection.close();

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top