gpt4 book ai didi

java - 转换为 JSON 时的空指针

转载 作者:搜寻专家 更新时间:2023-11-01 03:41:19 24 4
gpt4 key购买 nike

我在 Struts2 中使用 WDCalendar。我正在使用 GSON 将 Hashmap 转换为 Json String,但是突然之间,在将 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,

最佳答案

解决了:

某些事件字段值在数据库中为 NULL,这就是原因。

ret.put("events", eventArrayList);

eventArrayList 指的是 ArrayList<ArrayList> <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 值后一切正常。

关于java - 转换为 JSON 时的空指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13686335/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com