下面是net.sf.json.JSONException: java.lang.reflect.InvocationTargetException异常:
net.sf.json.JSONException: java.lang.reflect.InvocationTargetException
at net.sf.json.JSONObject.defaultBeanProcessing(JSONObject.java:818)
at net.sf.json.JSONObject._fromBean(JSONObject.java:699)
at net.sf.json.JSONObject.fromObject(JSONObject.java:172)
at net.sf.json.AbstractJSON._processValue(AbstractJSON.java:274)
at net.sf.json.JSONObject._processValue(JSONObject.java:2655)
at net.sf.json.JSONObject.processValue(JSONObject.java:2721)
at net.sf.json.JSONObject.setInternal(JSONObject.java:2736)
at net.sf.json.JSONObject.setValue(JSONObject.java:1424)
at net.sf.json.JSONObject.defaultBeanProcessing(JSONObject.java:765)
at net.sf.json.JSONObject._fromBean(JSONObject.java:699)
at net.sf.json.JSONObject.fromObject(JSONObject.java:172)
at net.sf.json.AbstractJSON._processValue(AbstractJSON.java:274)
at net.sf.json.JSONArray._processValue(JSONArray.java:2513)
at net.sf.json.JSONArray.processValue(JSONArray.java:2538)
at net.sf.json.JSONArray.addValue(JSONArray.java:2525)
at net.sf.json.JSONArray._fromCollection(JSONArray.java:1056)
at net.sf.json.JSONArray.fromObject(JSONArray.java:123)
at net.sf.json.AbstractJSON._processValue(AbstractJSON.java:240)
at net.sf.json.JSONObject._processValue(JSONObject.java:2655)
at net.sf.json.JSONObject.processValue(JSONObject.java:2721)
at net.sf.json.JSONObject.setInternal(JSONObject.java:2736)
at net.sf.json.JSONObject.setValue(JSONObject.java:1424)
at net.sf.json.JSONObject.defaultBeanProcessing(JSONObject.java:765)
at net.sf.json.JSONObject._fromBean(JSONObject.java:699)
at net.sf.json.JSONObject.fromObject(JSONObject.java:172)
at net.sf.json.JSONObject.fromObject(JSONObject.java:134)
at com.web.util.DBUtil.jsonObjectReturn(DBUtil.java:108)
at com.web.servlet.RepertoryCheckServlet.bsgBills(RepertoryCheckServlet.java:316)
at com.web.servlet.RepertoryCheckServlet.doPost(RepertoryCheckServlet.java:77)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at com.web.filter.FilterEconding.doFilter(FilterEconding.java:26)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:261)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:581)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:745)
net.sf.json.JSONException异常通常是因为成员变量数据类型不一致,转换为JSON数据时就会报异常,这次抛异常是因为从数据库查询出的时间数据类型Date没有从java.sql.Date转换为java.util.Date所导致,代码如下:
List
try {
StringBuffer buffer = new StringBuffer(selectBills);
con = DBUtil.getConnection();
ps = con.prepareStatement(buffer.toString());
rs = ps.executeQuery();
while (rs.next()) {
BillsInfo info = new BillsInfo();
info.setBillsID(rs.getInt("BillsID"));
info.setStaffMC(rs.getString("StaffMC"));
info.setEnteringTime(rs.getDate("EnteringTime"));
info.setRemarkBills(rs.getString("RemarkBills"));
billsInfos.add(info);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
DBUtil.close(con, ps, rs);
}
上面中BillsInfo类里面的enteringTime变量是java.util.Date类型,而通过jdbc从数据库查询出来的数据类型是java.sql.Date类型,虽然用set方法赋值是不会出现问题,但当用JSONObject转为JSON数据的时候就会报net.sf.json.JSONException异常,此时应该怎么改呢,只需要把rs.getDate("EnteringTime")改一下格式就不会再报这个异常了,如下:
Date date = new Date(rs.getDate("EnteringTime").getTime());
info.setEnteringTime(date);