0

I have a code in java that sends the data inside a CSV to a database. This works perfectly when I ran the main java method, but whenever I load my jsp, I get this error:

Type Exception Report

Message java.lang.NullPointerException

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception

org.apache.jasper.JasperException: java.lang.NullPointerException
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:517)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
Root Cause

java.lang.NullPointerException
    org.apache.jsp.csv_jsp.method(csv_jsp.java:85)
    org.apache.jsp.csv_jsp._jspService(csv_jsp.java:211)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:477)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
Note The full stack trace of the root cause is available in the server logs.

This is my JSP:

<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.io.BufferedReader" %>
<%@ page import="java.io.FileReader" %>
<%@ page import="java.io.IOException" %>
<%@ page import="java.sql.SQLException" %>
<html>
<head>
    <title>JSP</title>
</head>
<body>

<<input type="submit" onclick="<%method();%>"/>

<%!
     static void method (){

        String jdbcURL = "jdbc:mysql://localhost:3306/testDb";
        String username = "root";
        String password = "1234";

        String csvFilePath = "C:/Users/Name/Desktop/Attendance.csv";

        int batchSize = 20;

        Connection connection = null;

        try {

            connection = DriverManager.getConnection(jdbcURL, username, password);
            connection.setAutoCommit(false);

            String sql = "INSERT INTO csv (name, `in-time`, `out-time`) VALUES (?, ?, ?)";
            PreparedStatement statement = connection.prepareStatement(sql);

            BufferedReader lineReader = new BufferedReader(new FileReader(csvFilePath));
            String lineText = null;

            int count = 0;

            lineReader.readLine(); // skip header line

            while ((lineText = lineReader.readLine()) != null) {
                String[] data = lineText.split(",");
                String name = data[0];
                String inTime = data[1];
                String outTime = data[2];

                statement.setString(1, name);
                statement.setString(2, inTime);
                statement.setString(3, outTime);

                statement.addBatch();

                if (count % batchSize == 0) {
                    statement.executeBatch();
                }
            }

            lineReader.close();

            // execute the remaining queries
            statement.executeBatch();

            connection.commit();
            connection.close();

        } catch (IOException ex) {
            System.err.println(ex);
        } catch (SQLException ex) {
            ex.printStackTrace();

            try {
                connection.rollback();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }
%>
</body>
</html>

Intellij sends a hint near the rollback about this happening, but when I removed it, nothing happened. I got a blank page and the data wasnt going to the database either. Can anyone help with this?

pokerface0958
  • 85
  • 1
  • 8
  • @PradeepSimha unfortunately not. This seem to work fine when I used it in the java main class. Im not sure if I used it right in the jsp – pokerface0958 May 06 '20 at 09:28

1 Answers1

0

Few things:

  • Try putting some null-checks.
  • you're doing this modulo - "count % batchSize == 0", which I believe should be batchSize % count?
  • count is initially 0, and is never incremented.
Atul Kumar
  • 421
  • 3
  • 12