package HRCproject;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.gson.Gson;
import HRCproject.pojo.*;
import HRCproject.JDBC_Connection ;
@WebServlet("/fetch")
public class fetch_sevelet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException
{
res.setContentType("application/json");
res.setCharacterEncoding("UTF-8");
int max_rows = 500;
PrintWriter out = res.getWriter();
ArrayList<Invoice> data = new ArrayList<>();
//get connection
try {
Class.forName(JDBC_Connection.JDBC_DRIVER);
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
Connection con = DriverManager.getConnection(JDBC_Connection.URL,JDBC_Connection.USER,JDBC_Connection.PASS);
System.out.println("Connection Succesful");
String pageInURL = req.getParameter("page");
int page = Integer.parseInt(pageInURL) * max_rows;
// executing queries
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM winter_internship ORDER BY sl_no LIMIT " page);
while (rs.next())
{
Invoice s = new Invoice();
s.setSl_no(rs.getInt("sl_no"));
s.setBusiness_code(rs.getString("business_code"));
s.setCust_number(rs.getString("cust_number"));
s.setClear_date(rs.getString("clear_date"));
s.setBuisness_year(rs.getInt("buisness_year"));
String doc_id = rs.getString("doc_id");
doc_id.replace("0","");
s.setDoc_id(doc_id);
s.setPosting_date(rs.getDate("posting_date"));
s.setDue_in_date(rs.getDate("due_in_date"));
s.setBaseline_create_date(rs.getDate("baseline_create_date"));
s.setCust_payment_terms(rs.getString("cust_payment_terms"));
s.setInvoice_currency(rs.getString("invoice_currency"));
s.setDocument_type(rs.getString("document_type"));
s.setPosting_id(rs.getString("posting_id"));
s.setInvoice_id(rs.getInt("invoice_id"));
s.setTotal_open_amount(rs.getDouble("total_open_amount"));
data.add(s);
}
Gson gson = new Gson();
String invoices = gson.toJson(data);
out.print(invoices);
out.flush();
}
catch(SQLException e) {
e.printStackTrace();
}
catch(Exception e) {
e.printStackTrace();
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
Why am I getting this error? I am trying to get data from the database and then get the as json file.
I don't know where the error is there in the code if someone could help and figure out how to solve the problem. I have tried to replace the code in while clause with a different code which was working earlier so it seems there is not the problem in the while clause
CodePudding user response:
it must be because "gson.toJson(data);" this statement returning a null object and you're trying to store it in string format. that is why it giving you an error. try investigate it.
CodePudding user response:
It has to throws exception at Integer.parseInt() method, try to find why req.getParameter("page") returns null to String object.