0

I am working with Java and Postgresql server. I have never used it before. I am trying to create a REST API that receives http requests and stores them in a Postgresql server. Here is the endpoint I am trying to build.

@POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/validation")
    public Response Validation(C2BUtils c2BUtils) {

        PreparedStatement ps = null;
        Connection conn = null;
        conn = DBConnector.getPostgresSqlDBConnection();

        try {
            String insertValidationTxn = "INSERT INTO c2b(TransactionType,TransID,TransAmount,BusinessShortCode,BillRefNumber,InvoiceNumber,OrgAccountBalance,ThirdPartyTransID,MSISDN,FirstName,MiddleName,LastName,TransTime) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
            ps = conn.prepareStatement(insertValidationTxn);
            ps.setString(1, c2BUtils.getTransactionType());
            ps.setString(2, c2BUtils.getTransID());
            ps.setDouble(3, c2BUtils.getTransAmount());
            ps.setString(4, c2BUtils.getBusinessShortCode());
            ps.setString(5, c2BUtils.getBillRefNumber());
            ps.setString(6, c2BUtils.getInvoiceNumber());
            ps.setDouble(7, c2BUtils.getOrgAccountBalance());
            ps.setString(8, c2BUtils.getThirdPartyTransID());
            ps.setString(9, c2BUtils.getMsisdn());
            ps.setString(10, c2BUtils.getFirstName());
            ps.setString(11, c2BUtils.getMiddleName());
            ps.setString(12, c2BUtils.getLastName());
            ps.setString(13, c2BUtils.getTransTime());
            int add_detail = ps.executeUpdate();

            if (add_detail == 1) {
                System.out.println("New tx " + c2BUtils.getTransID() + " added");
            } else {
                System.out.println("no txn added");
            }

            conn.close(); 
        } catch (Exception e) {
            Logger lgr = Logger.getLogger(C2B.class.getName());
            lgr.log(Level.SEVERE, e.getMessage(), e);
            System.out.println("Message-->" + e.getMessage());
        }
        String re = "OK-2000";
        return Response.status(201).entity(re).build();
    }

Whenever I make an POST request , I get java.lang.NullPointerException and on the code it shows the error originates on this line

ps.setString(1, c2BUtils.getTransactionType());

I have tried to find out what the issue is without success. Any help will be appreciated.

bademba
  • 237
  • 4
  • 14
  • You need to send us the EXACT BYTES you are sending to do the POST HTTP command. – Pablo Santa Cruz May 15 '18 at 18:58
  • What do you mean... I can insert the same date from psql command line to postgesql db. The same insert command can't perform data insertion using preparedStatement – bademba May 15 '18 at 19:11
  • What do you mean... I can insert the same date from psql command line to postgesql db. The same insert command can't perform data insertion using preparedStatement. Can you identify the issue with the about stated line? – bademba May 15 '18 at 19:12
  • Thanks. I found the issue – bademba May 31 '18 at 15:42

0 Answers0