当我从动作中调用DAO方法时,我得到一个空指针异常。我不知道,为什么我明白了。当我检查代码时似乎是正确的 在行动类
public String getReport() {
System.out.println("Inside Action");
ExpenseReportBean bean=new ExpenseReportBean();
try {
getexpenseReport=reportdao.getReport();
return "success";
} catch (Exception e) {
e.printStackTrace();
return "success";
}
}
DAO班级:
public List<ExpenseReportBean> getReport() {
System.out.println("Inside DAO");
sqlQ = "select expense_date,SUM(IF(expense_name='salary',amount,0)) as salary,\n"
+ "SUM(IF(expense_name='rent',amount,0)) as rent\n"
+ "from am_expense\n"
+ "group by DATE_FORMAT(expense_date,'%d-%m-%Y')";
System.out.println("Inside DAO : SQL =" + sqlQ);
return jdbcTemplate.query(sqlQ, new RowMapper() {
public ExpenseReportBean mapRow(ResultSet rs, int i) throws SQLException {
ExpenseReportBean bean = new ExpenseReportBean();
bean.setSalary(rs.getString("salary"));
bean.setRent(rs.getString("rent"));
bean.setExpense_date(rs.getString("expense_date"));
System.out.println("Bean= "+rs.getString("rent"));
return bean;
}
});
}
XML文件:
<action name="getReportAction" class="com.ams.actions.ExpenseReportAction" method="getReport">
<result name="success" type="json">
<param name="root">getexpenseReport</param>
</result>
</action>
<bean id="expenseReportdao" class="com.ams.dao.ExpenseReportDao">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
输出:低于错误运行此代码
Execute
Info: Inside Action
Severe: java.lang.NullPointerException
at com.ams.actions.ExpenseReportAction.getReport(ExpenseReportAction.java:27)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)