table 单元格为空时的 Gson 空指针异常
Gson null pointer exception when table cell is empty
数据库table结构:
Card Number Name Bal Withdrawal Deposit Bal Tx Date Usage_Type
AL98****66325 MOSES 0 12981.02 1 -362764.96 16-Oct-14 N/A
AL98****66325 MOSES 0 50000.01 -362764.96 17-Oct-14 Manual
如您所见,"Deposit"
的第二行中的值为空。它不是零或空。所以我得到异常说:
java.lang.NullPointerException
atcom.google.gson.JsonPrimitive.isPrimitiveOrString(JsonPrimitive.java:278)
at com.google.gson.JsonPrimitive.setValue(JsonPrimitive.java:100)
at com.google.gson.JsonPrimitive.<init>(JsonPrimitive.java:65)
at com.In10s.getTable.GetTable.doGet(GetTable.java:49)
我从我的客户那里得到 xml 数据,可能有很多空值,我无法通过数百个文件来替换空值。有可能解决这个问题吗?
有没有gson的替代品。我还没有尝试过杰克逊图书馆。我不知道如何将结果集转换为 jackson 中的 json 对象 如果我必须转移。
Java代码:
public class GetTable extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
JsonObject jsonResponse = new JsonObject();
JsonArray data = new JsonArray();
PrintWriter out = response.getWriter();
Connection con = OracleDBConnection.getConnection();
String query = "Select * from CREDIT_CARD_TRANSACTIONS";
Statement st = con.createStatement();
ResultSet rSet = st.executeQuery(query);
while (rSet.next())
{
JsonArray row = new JsonArray();
row.add(new JsonPrimitive(rSet.getString("CARD_NUMBER")));
row.add(new JsonPrimitive(rSet.getString("FIRST_NAME")));
row.add(new JsonPrimitive(rSet.getString("OPENING_BALANCE")));
row.add(new JsonPrimitive(rSet.getString("WITHDRAWAL")));
row.add(new JsonPrimitive(rSet.getString("DEPOSIT")));
row.add(new JsonPrimitive(rSet.getString("CLOSING_BAL")));
row.add(new JsonPrimitive(rSet.getString("TXDATE")));
row.add(new JsonPrimitive(rSet.getString("USAGE_TYPE")));
data.add(row);
}
jsonResponse.add("ResponseData", data);
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
out.print(jsonResponse);
out.flush();
System.out.println(jsonResponse);
} catch (Exception e) {
e.printStackTrace();
}
}
我建议使用 ObjectMapper. You can download it from here。
首先将您的所有响应设置为映射列表或 POJO 列表:
List<Map<String, Object>> response = new ArrayList<>();// JDK7++
while (rSet.next())
{
Map<String, Object> row = new HashMap<>;
row.put("CARD_NUMBER", rSet.getString("CARD_NUMBER"));
// this wil be null safe
...
response.add(row);
}
当您填写回复列表时,将此列表转换为 Json 字符串:
ObjectMapper mapper = new ObjectMapper();
try {
response.getWriter().write(mapper.writeValueAsString(result));
out.flush();
} catch (JsonGenerationException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
数据库table结构:
Card Number Name Bal Withdrawal Deposit Bal Tx Date Usage_Type
AL98****66325 MOSES 0 12981.02 1 -362764.96 16-Oct-14 N/A
AL98****66325 MOSES 0 50000.01 -362764.96 17-Oct-14 Manual
如您所见,"Deposit"
的第二行中的值为空。它不是零或空。所以我得到异常说:
java.lang.NullPointerException
atcom.google.gson.JsonPrimitive.isPrimitiveOrString(JsonPrimitive.java:278)
at com.google.gson.JsonPrimitive.setValue(JsonPrimitive.java:100)
at com.google.gson.JsonPrimitive.<init>(JsonPrimitive.java:65)
at com.In10s.getTable.GetTable.doGet(GetTable.java:49)
我从我的客户那里得到 xml 数据,可能有很多空值,我无法通过数百个文件来替换空值。有可能解决这个问题吗?
有没有gson的替代品。我还没有尝试过杰克逊图书馆。我不知道如何将结果集转换为 jackson 中的 json 对象 如果我必须转移。
Java代码:
public class GetTable extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
JsonObject jsonResponse = new JsonObject();
JsonArray data = new JsonArray();
PrintWriter out = response.getWriter();
Connection con = OracleDBConnection.getConnection();
String query = "Select * from CREDIT_CARD_TRANSACTIONS";
Statement st = con.createStatement();
ResultSet rSet = st.executeQuery(query);
while (rSet.next())
{
JsonArray row = new JsonArray();
row.add(new JsonPrimitive(rSet.getString("CARD_NUMBER")));
row.add(new JsonPrimitive(rSet.getString("FIRST_NAME")));
row.add(new JsonPrimitive(rSet.getString("OPENING_BALANCE")));
row.add(new JsonPrimitive(rSet.getString("WITHDRAWAL")));
row.add(new JsonPrimitive(rSet.getString("DEPOSIT")));
row.add(new JsonPrimitive(rSet.getString("CLOSING_BAL")));
row.add(new JsonPrimitive(rSet.getString("TXDATE")));
row.add(new JsonPrimitive(rSet.getString("USAGE_TYPE")));
data.add(row);
}
jsonResponse.add("ResponseData", data);
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
out.print(jsonResponse);
out.flush();
System.out.println(jsonResponse);
} catch (Exception e) {
e.printStackTrace();
}
}
我建议使用 ObjectMapper. You can download it from here。
首先将您的所有响应设置为映射列表或 POJO 列表:
List<Map<String, Object>> response = new ArrayList<>();// JDK7++
while (rSet.next())
{
Map<String, Object> row = new HashMap<>;
row.put("CARD_NUMBER", rSet.getString("CARD_NUMBER"));
// this wil be null safe
...
response.add(row);
}
当您填写回复列表时,将此列表转换为 Json 字符串:
ObjectMapper mapper = new ObjectMapper();
try {
response.getWriter().write(mapper.writeValueAsString(result));
out.flush();
} catch (JsonGenerationException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}