my dog learned polymorphism
The moose likes Servlets and the fly likes initialising db connection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "initialising db connection " Watch "initialising db connection " New topic

initialising db connection

nikil shar
Ranch Hand

Joined: May 25, 2008
Posts: 116
hi all,
have got a servlet which calls a utilities class to initialize db connection. inside the utilities class my method looks like this :

when i call this method from my servlet :

i get a null pointer returned. i can see that "dbUtil.establishDBConnection()" returns an object but for some reason it never gets assigned to 'con' object in my servlet !!

any help would be much appreciated.

Deepak Bala

Joined: Feb 24, 2006
Posts: 6662

public void establishDBConnection()

The return type is void, so I doubt if your code compiles

SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Somnath Mallick
Ranch Hand

Joined: Mar 04, 2009
Posts: 482
Where is the return statement in establishDBConnection()??
Sri Dharan
Ranch Hand

Joined: Aug 21, 2008
Posts: 74
It should be return Connection object :
Deepak Bala

Joined: Feb 24, 2006
Posts: 6662

You are probably getting the DS on the first try and assigning the connection when the DS is not null on the second try. The second call probably does not return null, but we can only speculate since the code and the problem at hand do not match each other
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Importantly Change your class name from dbUtilities to DBUtilities
I agree. Here's the link:
subject: initialising db connection
It's not a secret anymore!