当表格单元格为空时,Gson空指针异常

时间:2015-06-29 06:07:00

标签: java jackson gson

数据库表结构:

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数据,可能存在空值,我无法通过数百个文件来替换emtpy值。有可能解决这个问题吗?

gson有什么替代品吗?我还没有尝试过杰克逊图书馆。我不知道如何将结果集转换为杰克逊的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();
        }
    }

1 个答案:

答案 0 :(得分:1)

我建议使用ObjectMapper。您可以从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);
            }

当您填写响应列表时,将此List转换为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();

}