Results 1 to 15 of 15
  1. #1
    samuel.roshni is offline Member
    Join Date
    Nov 2010
    Posts
    1
    Rep Power
    0

    Default Null pointer exception

    My jdbc code is as follows

    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 connection is gettin created.. put it givea me a null pointer exception at releaseConnection()..

    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

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Please use code tags next time, when you are posting code segments in the forum. Unformated codes are really hard to read.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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.

  4. #4
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    837
    Rep Power
    6

    Default

    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:

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by RamyaSivakanth View Post
    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.
    Oh yes, a lot. Small change can make a big difference, like the query not return data at all. Validations are tricky at that point.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,036
    Rep Power
    20

    Default

    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?

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,036
    Rep Power
    20

    Default

    Quote Originally Posted by RamyaSivakanth View Post
    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.
    The rest is good advice, but I'm not sure I understand what you're saying here?

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  9. #9
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    837
    Rep Power
    6

    Default

    ya exactly Eranga as you said .
    If the record has null values ,then setter may end up in trouble.
    Ramya:cool:

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,036
    Rep Power
    20

    Default

    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. #11
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    837
    Rep Power
    6

    Default

    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:

  12. #12
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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...

  13. #13
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    837
    Rep Power
    6

    Default

    Ok fine .Come up with complete formatted code with s.o.p and proper comments.
    Ramya:cool:

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,036
    Rep Power
    20

    Default

    Quote Originally Posted by RamyaSivakanth View Post
    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.
    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.

  15. #15
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Null pointer Exception
    By peiceonly in forum New To Java
    Replies: 8
    Last Post: 09-05-2010, 06:48 PM
  2. Help with Null Pointer Exception
    By Beginner in forum New To Java
    Replies: 2
    Last Post: 04-17-2010, 04:41 PM
  3. Null pointer exception
    By izzy in forum New To Java
    Replies: 5
    Last Post: 03-22-2010, 05:19 PM
  4. Null Pointer exception
    By diegoyj in forum New To Java
    Replies: 7
    Last Post: 01-29-2010, 04:17 PM
  5. Null Pointer Exception
    By Jacinth in forum New To Java
    Replies: 4
    Last Post: 01-22-2009, 01:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •