Announcement Announcement Module
Collapse
No announcement yet.
Null Pointer exception in jdbcTemplate Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Null Pointer exception in jdbcTemplate

    Hi All,

    I am using jdbcTemplate to do database operations. I am facing an issue intermittently. While iterating row mapper resultset, if value from database is NULL then my code raise NPE. Can someone please suggest me how do i get rid of this while setting VO's

    Code:
    result = jdbcTemplate.query(
    				SQLConstantHolder.FETCH_PRODUCT_DETAIL_BY_USER,
    				new Object[] { Integer.valueOf(userId) },
    				new RowMapper<ProductVO>() {
    					public ProductVO mapRow(ResultSet resultSet, int rowNum)
    							throws SQLException {
    						ProductVO productVO = new ProductVO();
    						productVO.setFoodId(resultSet.getInt(1));
    						productVO
    								.setFoodName(resultSet.getString(2).toString());
    						productVO.setFoodSubname(resultSet.getString(3)
    								.toString());
    						productVO
    								.setFoodDesc(resultSet.getString(4).toString());
    						return productVO;
    					}
    				});
    Error stack trace is -

    Code:
    java.lang.NullPointerException
    	at XXX.XXX.dao.impl.ProductDAOImpl$2.mapRow(ProductDAOImpl.java:92)
    	at XXX.XXX.dao.impl.ProductDAOImpl$2.mapRow(ProductDAOImpl.java:1)
    	at org.springframework.jdbc.core.RowMapperResultSetExtractor.extractData(RowMapperResultSetExtractor.java:92)
    	at org.springframework.jdbc.core.RowMapperResultSetExtractor.extractData(RowMapperResultSetExtractor.java:1)
    	at org.springframework.jdbc.core.JdbcTemplate$1.doInPreparedStatement(JdbcTemplate.java:648)
    	at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:586)
    	at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:636)
    	at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:665)
    	at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:673)
    	at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:713)
    	at XXX.XXX.dao.impl.ProductDAOImpl.fetchProduct(ProductDAOImpl.java:81)
    	at XXX.XXX.service.impl.ProductServiceImpl.fetchProduct(ProductServiceImpl.java:72)
    	at XXX.XXX.service.impl.ProductServiceImpl.fetchProductData(ProductServiceImpl.java:37)
    	at XXX.XXX.restaurant.controller.ProductManagerController.fetchProductInfo(ProductManagerController.java:39)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    	at java.lang.reflect.Method.invoke(Unknown Source)

  • #2
    Well your code is flawed... Why are you calling toString after a getString?! You already have a string (unless it is null which is what happens in your case).

    Comment


    • #3
      aahhhh....right, silly mistake though :P. Thank you so much for pointing out.

      Comment

      Working...
      X