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.