Struts2でWDCalendarを使用しています。GSONを使用してHashmapをJsonStringに変換していましたが、突然、HashmapをJSON Stringに変換しているときに、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;
}
変換に渡す前の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,