JSP Servlet Null异常

时间:2018-02-28 22:05:36

标签: java mysql jsp servlets

我的简单项目有问题(使用JSP)。 当我尝试运行我的servlet时,我收到了这条消息:

User::find($user_id)->load('measurements.metas.unit')

我不知道我做错了什么。我已经尝试从连接的数据库中获取数据(当然它已经填满了)并且我有这个例外。

这是我的代码: 1)患者类

Type Exception Report

Message java.lang.NullPointerException

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

Exception

javax.servlet.ServletException: java.lang.NullPointerException
com.dentapp.web.jdbc.PatientControllerServlet.doGet(PatientControllerServlet
   .java:42)
javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
Root Cause

java.lang.NullPointerException
com.dentapp.web.jdbc.PatientDBUtil.getPatients(PatientDBUtil.java:28)
com.dentapp.web.jdbc.PatientControllerServlet.listPatients(PatientController
Servlet.java:50)
com.dentapp.web.jdbc.PatientControllerServlet.doGet(PatientControllerServlet
.java:40)
javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
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.

2)PatientDBUtil

public class Patient {

private int id;
private String firstName;
private String lastName;
private String email;

public Patient(int id, String firstName, String lastName, String email) {
    this.id = id;
    this.firstName = firstName;
    this.lastName = lastName;
    this.email = email;
}

public Patient(String firstName, String lastName, String email) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.email = email;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getFirstName() {
    return firstName;
}

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

public String getLastName() {
    return lastName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

@Override
public String toString() {
    return "Patient{" +
            "id=" + id +
            ", firstName='" + firstName + '\'' +
            ", lastName='" + lastName + '\'' +
            ", email='" + email + '\'' +
            '}';
}
}

以下是PatientControllerServlet。

public class PatientDBUtil {

private DataSource dataSource;

public PatientDBUtil(DataSource theDataSource){
    dataSource = theDataSource;
}

public List<Patient> getPatients() throws Exception{

    List<Patient> patients = new ArrayList<>();

    Connection myConn=null;
    Statement myStmt= null;
    ResultSet myRs = null;

    try {
        //get a connection
        myConn = dataSource.getConnection();

        // create sql statment
        String sql = "select id_pacjenta, imie, nazwisko, email from pacjent 
 order by nazwisko";
        //execute query
        myStmt = myConn.createStatement();
        // process result set
        myRs = myStmt.executeQuery(sql);
        // close JDBC objects
        while(myRs.next()){

            // retrieve data from result set row
            int id = myRs.getInt("id_pacjenta");
            String firstName = myRs.getString("imie");
            String lastName = myRs.getString("nazwisko");
            String email = myRs.getString("email");
            //create new patient object
            Patient tempPatient = new Patient(id, firstName, lastName, 
  email);
            //add it to the list of patients
            patients.add(tempPatient);
        }
        return patients;
    }

    finally{
        //close JDBC objects
        close(myConn, myStmt, myRs);


    }
}

private void close(Connection myConn, Statement myStmt, ResultSet myRs) {

    try{
        if(myRs !=null){
            myRs.close();
        }
        if(myStmt !=null){
            myStmt.close();
        }
        if(myConn !=null){
            myConn.close();
        }
    }
    catch(Exception exc){
        exc.printStackTrace();
    }
   }

我是这项技术的新手,我不明白这里的错误是什么。

0 个答案:

没有答案