0

I am creating an appointment page. I have used java and MySQL as my database. I am having trouble in adding new appointments to the database. I can update or delete my previously added data in the database, but i am facing trouble in adding new appointments.

SEVERE: Servlet.service() for servlet [model.AppointmentsAPI] in context with path [/PracticalTest_Doctor_Management] threw exception
java.lang.NullPointerException
at model.AppointmentAPI.doPut(AppointmentAPI.java:73)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:663)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)

This is the error i am getting "PracticalTest_Doctor_Management" is my project name. The 73rd line is the doPut method in AppointmentAPI.java. Below I have attached the code.

For database connectivity:

DB_Connection.java

package Util;
import java.sql.Connection;
import java.sql.DriverManager;

    public class DB_Connection {
        
        
        public Connection connect(){
            Connection con = null;
            
            try {
                Class.forName("com.mysql.jdbc.Driver");
                con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/healthmgt", "root", "");
                
                // For testing print success message
                            System.out.println("Successfully Connected");
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        return con;
    
                    }
            
    
    }

The Insert code i am using:

Appointment.java

public String insertAppointment(String date, String venue, String docName, String patientId) {
        String output = "";
        try {
            DB_Connection obj_DB_Connection= new DB_Connection();
            Connection con = obj_DB_Connection.connect();
            if (con == null) {
                return "Error while connecting to the database for inserting appoinment details.";
            }

            String query = " insert into appointment (`app_Id`,`app_Date`,`app_Venue`,`app_Doctor_Id`,`app_Patient_Id`) values (?,?,?,?,?)";
            PreparedStatement preparedStatement = con.prepareStatement(query);

            preparedStatement.setInt(1, 0);
            preparedStatement.setString(2, date);
            preparedStatement.setString(3, venue);
            preparedStatement.setString(4, docName);
            preparedStatement.setString(5, patientId);

            preparedStatement.execute();

            String newAppointment = readAppoinment();
            output = "{\"status\":\"success\", \"data\": \"" + newAppointment + "\"}";

        } catch (Exception e) {

            output = "{\"status\":\"error\", \"data\": \"Error while creating appointment.\"}";
            System.err.println(e.getMessage());
        }
        return output;
    }

AppointmentAPI.java

protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    String output = appointmentObj.insertAppointment(request.getParameter("appointmentDate"),
            request.getParameter("appointmentVenue"), request.getParameter("assignDoctor"),
            request.getParameter("assignPatient"));

    response.getWriter().write(output);

}

protected void doPut(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        Map paras = getParasMap(request);

        String output = appointmentObj.updateAppoinment(paras.get("hidapp_IdSave").toString(),
                paras.get("appointmentDate").toString(), 
                paras.get("appointmentVenue").toString(),
                paras.get("assignDoctor").toString(), 
                paras.get("assignPatient").toString());

        // There is a Error when updating an appointment. Use only date when updating
        // otherwise it prints the "Data truncation" error in
        // the console (yyyy/mm//dd) format

        response.getWriter().write(output);
    }

Please look at the link of the image I have attached below, I uploaded it as a link because i am unable to upload it as an image. The Image below shows the error description.

Can Someone help me with this !!!

Manoj
  • 1
  • 3

1 Answers1

0

The exception happens in doPut() method if you didn't implement it tries to forward to doPost() or review the coming request

  @Override
  protected void doPut(HttpServletRequest req, HttpServletResponse resp)
          throws ServletException, IOException {
      doPost(req, resp);
  }
0xh3xa
  • 4,801
  • 2
  • 14
  • 28
  • yahh the error also shows that point, but i am unable to find an error in the doPut method. if possible please look at the image i have upload. it is there as s link – Manoj Jul 07 '20 at 10:07