-1

I am having one query.That query is having an input,If the input found in Database then it will return sum value. If the input is not found in db then it will throw exception.

String Query="SELECT SUM(tot) FROM Test where Id=?";

double sum = getJdbcTemplate().queryForObject(
        Query, new Object[] { Id }, Double.class);

return sum;

If the Id present In db then It will return sum value.Otherwise It will return NullPointerException.I want to Handle that exception.If I got NullPointer then I want to set Sum value as 0.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
Sai Kishore Mani
  • 49
  • 1
  • 3
  • 11

1 Answers1

0

You could use a java.lang.Double instead of a primitive double, and then programatically check for null:

String Query="SELECT SUM(tot) FROM Test where Id=?";

Double sum = getJdbcTemplate().queryForObject(
             Query, new Object[] { Id }, Double.class);

return Optional.ofNullable(sum).orElse(0.0);
Mureinik
  • 297,002
  • 52
  • 306
  • 350