0

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();
        }
    }
kittu
  • 6,662
  • 21
  • 91
  • 185

1 Answers1

1

I would advise using ObjectMapper. You can download it from here.

First set all your response into a List of Maps or a list of POJO's:

 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);
            }

When you will fill your list of responses, convert this List into a Json String:

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();

}
kittu
  • 6,662
  • 21
  • 91
  • 185
Beri
  • 11,470
  • 4
  • 35
  • 57
  • Error at this line:` jsonResponse.add("ResponseData", mapper.writeValueAsString(response));` Cannot find method `JSONObject.add(String,String)` – kittu Jun 29 '15 at 07:59
  • Write String representation to the output directly, it should work, I updated my code. – Beri Jun 29 '15 at 08:06
  • `error: cannot access ObjectCodec out.print(mapper.writeValueAsString(response)); class file for org.codehaus.jackson.ObjectCodec not found` – kittu Jun 29 '15 at 08:09
  • Have you added this jar to classpath? – Beri Jun 29 '15 at 08:10
  • This is the jar I am using: `com.springsource.org.codehaus.jackson.mapper-1.4.2.jar` Is this the right one? – kittu Jun 29 '15 at 08:12
  • Added link in my answer. – Beri Jun 29 '15 at 08:18
  • Removed old one and Added the jar you have provided and Still getting the same error: `error: cannot access ObjectCodec out.print(mapper.writeValueAsString(response)); class file for org.codehaus.jackson.ObjectCodec not found` – kittu Jun 29 '15 at 08:27
  • Fixed the jar file issue from this SO link: http://stackoverflow.com/questions/28127436/json-decode-in-java. However, when I run the program its not returning any response – kittu Jun 29 '15 at 08:36
  • I get a new error now: `com.fasterxml.jackson.databind.JsonMappingException: getWriter() has already been called for this response (through reference chain: org.apache.catalina.connector.ResponseFacade["outputStream"])` – kittu Jun 29 '15 at 08:39
  • Tried the updated code with flush. Still same problem – kittu Jun 29 '15 at 08:48
  • Just figured it out, replace print with write. – Beri Jun 29 '15 at 08:51
  • `out.write(mapper.writeValueAsString(response));`?? – kittu Jun 29 '15 at 08:52
  • ys, just as in my respose, there should be a write method in writer. You can even go with: response.getWriter().write(mapper.writeValueAsString(response)); – Beri Jun 29 '15 at 08:53
  • Still the same error: `getWriter() has already been called for this response (through reference chain: org.apache.catalina.connector.ResponseFacade["outputStream"])` – kittu Jun 29 '15 at 08:55
  • Fixed it. Thanks a lot. The problem got fixed when I replaced it with this line: `response.getWriter().write(mapper.writeValueAsString(result));` – kittu Jun 29 '15 at 09:49