Database table structure:
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
As you can see the value in the second row for "Deposit"
is empty. It is not zero or null. So I am getting exception saying:
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)
I get the xml data from my clients and there might be so empty values and I can't go through hundreds of files to replace emtpy values. Is it possible to fix this?
Is there any alternative to gson. I haven't tried jackson library. I don't know how to convert result set to json object in jackson If I have to shift.
Java code:
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();
}
}