Results 1 to 15 of 15
Thread: Null pointer exception
- 11-24-2010, 06:03 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 1
- Rep Power
- 0
Null pointer exception
My jdbc code is as follows
The connection is gettin created.. put it givea me a null pointer exception at releaseConnection()..Java Code:public ArrayList<IncentiveBean> displayIncentive(int empNo, int year) { ArrayList<IncentiveBean> ilist = new ArrayList<IncentiveBean>(); try { makeConnection(); pst = conn .prepareStatement("SELECT p.project_id,p.project_name,e.proj_incentive FROM employee_project e, project_master p WHERE p.project_id=e.project_id AND emp_id=? AND e.allocation_date<TO_DATE(?) AND e.release_date>TO_DATE(?)"); pst.setInt(1, empNo); pst.setString(2, "31-Mar-" + year); pst.setString(3, "1-Apr-" +(year - 1)); ResultSet rs = pst.executeQuery(); while (rs.next()) { IncentiveBean inc = new IncentiveBean(); inc.setProjId(rs.getString(1)); inc.setProjName(rs.getString(2)); inc.setProjIncentive(rs.getDouble(3)); ilist.add(inc); } releaseConnection(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); releaseConnection(); } return ilist; }
The exception occurs at times.. otherwise the code executes successfully.. i am not settin any null value to the variables
Please help..Last edited by Eranga; 11-25-2010 at 06:25 AM. Reason: code tags added
- 11-25-2010, 06:26 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Please use code tags next time, when you are posting code segments in the forum. Unformated codes are really hard to read.
- 11-25-2010, 06:29 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
And also please provide more details about the question, like complete error message, and the relevant codes like releaseConnection() implementation and so on. Otherwise it's bit difficult to comment on your question.
- 11-25-2010, 08:15 AM #4
There are lot of chances ur code will end up in problem.
1. Inside resultset loop,if no proper data is there then setter will have trouble.
2. why you have 2 release connection method here? Place the connection in finally block.
so whether the exception occurs or not occurs finally will be executed once.
3. check if the connection = null or not before calling release connection.
Put s.o.p for debugging.
As Eranga suggested,format the code.Ramya:cool:
- 11-25-2010, 08:49 AM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 11-25-2010, 09:12 AM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
I suspect (as Ramya points out) that it's the two releaseConnection calls.
Just have the one, in a finally block. That should do you.
Also, as Eranga asks, what does the code in releaseConnection() look like?
Do you check whether the conn is already null or not before trying to close it?
- 11-25-2010, 09:12 AM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
- 11-25-2010, 09:16 AM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
I think it's about manipulating dataset in a while loop.
- 11-25-2010, 09:21 AM #9
ya exactly Eranga as you said .
If the record has null values ,then setter may end up in trouble.Ramya:cool:
- 11-25-2010, 09:31 AM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Why?
This is a perfectly normal way of populating a List from a result set.
You loop round the result set, creating an instance for each row, populating the values based on the getXXX() from the result set. Then add that instance to the List.
Or am I missing something in this instance?
- 11-25-2010, 09:42 AM #11
Instantiating the object and adding to the list is perfectly right.
what iam saying here is about return type of resultset and setting it inside setter method.
Error occurence is very less ( as Tolls suggested). But, for code improvement I have given a tip.Ramya:cool:
- 11-25-2010, 09:47 AM #12
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
For me there is no error (however some are confusing), but it leads to empty list. If the list traverse in random sequence after that in a different case...
- 11-25-2010, 09:58 AM #13
Ok fine .Come up with complete formatted code with s.o.p and proper comments.
Ramya:cool:
- 11-25-2010, 10:18 AM #14
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Ah. I hadn't noticed one of the columns is a double.
You're saying if column 3 is null then the call to getDouble() will give us 0, which may not be correct?
I have to say I never did understand why those weren't defined as returning Double or Integer, rather than primitives.
Then again, I always use getBigDecimal and getBigInteger.
-
Hijack post deleted. Poster, if you have a question, please ask it in a new thread. Locking thread.
Similar Threads
-
Null pointer Exception
By peiceonly in forum New To JavaReplies: 8Last Post: 09-05-2010, 06:48 PM -
Help with Null Pointer Exception
By Beginner in forum New To JavaReplies: 2Last Post: 04-17-2010, 04:41 PM -
Null pointer exception
By izzy in forum New To JavaReplies: 5Last Post: 03-22-2010, 05:19 PM -
Null Pointer exception
By diegoyj in forum New To JavaReplies: 7Last Post: 01-29-2010, 04:17 PM -
Null Pointer Exception
By Jacinth in forum New To JavaReplies: 4Last Post: 01-22-2009, 01:47 PM


LinkBack URL
About LinkBacks


Bookmarks