Java action类调用DAO的服务方法时出现空指针异常
当我从action调用DAO的方法时,我得到一个空指针异常。我不知道,为什么我会得到它。当我检查代码时,它似乎是正确的 行动课Java action类调用DAO的服务方法时出现空指针异常,java,spring,Java,Spring,当我从action调用DAO的方法时,我得到一个空指针异常。我不知道,为什么我会得到它。当我检查代码时,它似乎是正确的 行动课 public String getReport() { System.out.println("Inside Action"); ExpenseReportBean bean=new ExpenseReportBean(); try { getexpenseReport=reportdao.getReport();
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;
}
});
}
你可以检查reportdao对象和它的bean配置之间的连接吗。我猜这个对象是空的。我检查它。这是bean配置:它不是通过使用reportdao instancehi Sathish的getter/setter方法自动创建对象。你能粘贴你的整个动作类吗?你在哪里使用bean引用ExpenseReportBean=newExpenseReportBean();?您已经创建了一个对象,需要在该对象上像bean.getReport()一样调用r8;你可以检查reportdao对象和它的bean配置之间的连接吗。我猜这个对象是空的。我检查它。这是bean配置:它不是通过使用reportdao instancehi Sathish的getter/setter方法自动创建对象。你能粘贴你的整个动作类吗?你在哪里使用bean引用ExpenseReportBean=newExpenseReportBean();?您已经创建了一个对象,需要在该对象上像bean.getReport()一样调用r8;
<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)