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);
}
Can Someone help me with this !!!