我在Struts2中使用WDCalendar。 我正在使用GSON将Hashmap转换为Json String,但突然之间,我在将Hashmap转换为JSON字符串时获得了NULL指针异常
result = new Gson().toJson(ret);
我的代码:
public static String loadAllEvents(String start, String end) throws SQLException {
PreparedStatement ps1 = null;
ResultSet rs1 = null;
Gson gson = new Gson();
String result = "";
Connection con = ULDBConnectionUtility.getDBConnection();
HashMap<String, Object> ret = new HashMap<String, Object>();
try{
String strSql = "SELECT * FROM EVENT_MANAGER";// WHERE START_DATE BETWEEN '"+start+"' AND '"+end+"'";
ps1 = con.prepareStatement(strSql);
rs1 = ps1.executeQuery();
SimpleDateFormat fromDB = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
java.text.SimpleDateFormat sourceFormat = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss ZZZ");
ArrayList<ArrayList> eventArrayList = new ArrayList<ArrayList>();
while(rs1.next()){
ArrayList EvtData = new ArrayList();
EvtData.add(rs1.getInt("EVENT_ID"));//0
EvtData.add(rs1.getString("EVENT_TITLE"));//1
EvtData.add(sourceFormat.format(fromDB.parse(rs1.getString("START_DATE"))));//2
EvtData.add(sourceFormat.format(fromDB.parse(rs1.getString("END_DATE"))));//3
EvtData.add(rs1.getInt("IS_ALL_DAY_EVENT"));//4
EvtData.add(Integer.parseInt("0"));//5
EvtData.add(rs1.getInt("RECURRING_RULE"));//6
EvtData.add(rs1.getString("COLOR"));//7
EvtData.add(Integer.parseInt("1"));//8
EvtData.add(rs1.getString("LOCATION"));//9
EvtData.add("");//10
eventArrayList.add(EvtData);
}
String error = "";
ret.put("events", eventArrayList);
ret.put("issort", true);
ret.put("start", sourceFormat.format(fromDB.parse(start)));
ret.put("end", sourceFormat.format(fromDB.parse(end)));
ret.put("error", error);
System.out.println("ret : "+ret);
result = new Gson().toJson(ret);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (rs1 != null) {
rs1.close();
}
if (ps1 != null) {
ps1.close();
}
if (con != null) {
con.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return result;
}
传递给convertion之前的HashMap:
{events=[[98, EvebtTitle, Mon Dec 03 2012 10:45:00 +0530, Wed Oct 17 2012 11:15:00 +0530, 0, 0, 0, 0, 1, NJ, ], [118, testDec, Wed Dec 05 2012 09:00:00 +0530, Wed Dec 05 2012 10:00:00 +0530, 0, 0, 0, null, 1, null, ], [67, Test45, Wed Dec 05 2012 10:30:00 +0530, Tue Oct 16 2012 11:00:00 +0530, 1, 0, 0, 0, 1, NY, ], [119, sd, Tue Dec 04 2012 00:00:00 +0530, Tue Dec 04 2012 00:00:00 +0530, 1, 0, 0, null, 1, null, ], [78, Test, Fri Dec 07 2012 09:30:00 +0530, Mon Oct 15 2012 10:15:00 +0530, 0, 0, 0, 1, 1, LA, ], [66, Event 2, Tue Dec 04 2012 10:00:00 +0530, Tue Oct 16 2012 10:30:00 +0530, 0, 0, 1, 0, 1, CA, ]], start=Thu Jan 01 1970 05:30:00 +0530, issort=true, end=Thu Jan 01 1970 05:30:00 +0530, error=null}
错误:
20:49:15,597 ERROR [STDERR] java.lang.NullPointerException
20:49:15,599 ERROR [STDERR] at com.google.gson.DefaultTypeAdapters$CollectionTypeAdapter.serialize(DefaultTypeAdapters.java:443)
20:49:15,601 ERROR [STDERR] at com.google.gson.DefaultTypeAdapters$CollectionTypeAdapter.serialize(DefaultTypeAdapters.java:430)
20:49:15,604 ERROR [STDERR] at com.google.gson.JsonSerializationVisitor.visitUsingCustomHandler(JsonSerializationVisitor.java:128)
20:49:15,605 ERROR [STDERR] at com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:96)
20:49:15,606 ERROR [STDERR] at com.google.gson.JsonSerializationContextDefault.serialize(JsonSerializationContextDefault.java:47)
20:49:15,608 ERROR [STDERR] at com.google.gson.DefaultTypeAdapters$CollectionTypeAdapter.serialize(DefaultTypeAdapters.java:445)
20:49:15,609 ERROR [STDERR] at com.google.gson.DefaultTypeAdapters$CollectionTypeAdapter.serialize(DefaultTypeAdapters.java:430)
20:49:15,610 ERROR [STDERR] at com.google.gson.JsonSerializationVisitor.visitUsingCustomHandler(JsonSerializationVisitor.java:128)
20:49:15,611 ERROR [STDERR] at com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:96)
20:49:15,612 ERROR [STDERR] at com.google.gson.JsonSerializationContextDefault.serialize(JsonSerializationContextDefault.java:47)
20:49:15,613 ERROR [STDERR] at com.google.gson.DefaultTypeAdapters$MapTypeAdapter.serialize(DefaultTypeAdapters.java:509)
20:49:15,613 ERROR [STDERR] at com.google.gson.DefaultTypeAdapters$MapTypeAdapter.serialize(DefaultTypeAdapters.java:489)
20:49:15,614 ERROR [STDERR] at com.google.gson.JsonSerializationVisitor.visitUsingCustomHandler(JsonSerializationVisitor.java:128)
20:49:15,615 ERROR [STDERR] at com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:96)
20:49:15,616 ERROR [STDERR] at com.google.gson.JsonSerializationContextDefault.serialize(JsonSerializationContextDefault.java:47)
20:49:15,617 ERROR [STDERR] at com.google.gson.Gson.toJson(Gson.java:269)
20:49:15,618 ERROR [STDERR] at com.google.gson.Gson.toJson(Gson.java:220)
20:49:15,618 ERROR [STDERR] at com.google.gson.Gson.toJson(Gson.java:200)
20:49:15,619 ERROR [STDERR] at dao.UtilityDao.loadAllEvents(UtilityDao.java:174)
20:49:15,620 ERROR [STDERR] at utility.CalendarUtility.loadAllList(CalendarUtility.java:64)
20:49:15,621 ERROR [STDERR] at servlet.DataFeed.doGet(DataFeed.java:56)
20:49:15,622 ERROR [STDERR] at servlet.DataFeed.doPost(DataFeed.java:97)
20:49:15,623 ERROR [STDERR] at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
20:49:15,624 ERROR [STDERR] at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
20:49:15,625 ERROR [STDERR] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
20:49:15,626 ERROR [STDERR] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
20:49:15,627 ERROR [STDERR] at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
20:49:15,
答案 0 :(得分:1)
解决了:
某些事件字段值在DB中为NULL,这就是原因。
在ret.put("events", eventArrayList);
eventArrayList引用ArrayList<ArrayList>
,<ArrayList>
中的几个字段值为NULL,
从上面Ex :(查看内部arraylist中第二个索引处的Null值)
{events=[[98, EvebtTitle, Mon Dec 03 2012 10:45:00 +0530, Wed Oct 17 2012 11:15:00 +0530, 0, 0, 0, 0, 1, NJ, ], [118, testDec, Wed Dec 05 2012 09:00:00 +0530, Wed Dec 05 2012 10:00:00 +0530, 0, 0, 0, null, 1, null, ]]}
从内部ArrayList中删除Null值后,一切正常。
答案 1 :(得分:0)
关于方法
,我想到了一件事public String toJson(Object src)
是GSON documentation,表示“当指定的对象不是泛型时,应该使用此方法。如果对象是泛型类型,请使用toJson(Object,Type)。”
另一件事是空指针异常可能是由任何其他对象引起的,而不是特别是HashMap。您似乎认为它是理所当然的,但我建议您调试代码。