我有一個查詢,以便從單個列總結數值:JDBC模板引發NullPointerException異常時沒有行發現
int numberOfDays = jdbcTemplate.queryForObject
("select sum(number_of_days) from business_trip where name = ? and surname = ? and type = ?",new Object[]{name,surname,"UE"},Integer.class);
它工作正常,只要有UE類別的選定用戶出差。然後它正確地計算旅行天數。但是,如果沒有UE類別的行程,而不是零,我得到NullPointerException。我可以將此行放在try/catch中,但這是一個解決方法。我想弄清楚爲什麼NPE在這種情況下被拋出。
*2016-11-24 11:13:02.613 ERROR 8316 --- [nio-8080-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause java.lang.NullPointerException: null
at com.tfb.dao.BusinessTripDAO.getKrajoweTripsDaysForEmployee(BusinessTripDAO.java:59) ~[classes/:na]*
和線路59是一個用'jdbcTemplate.queryForObject'? – pleft
這是正確的 – jarosik