我正在创建一个执行 sql 查询的 servlet,它的基本版本可以工作,但是当我添加 NOT IN 语句时,它会返回一个空指针异常。我宁愿它不这样做,而是显示一条消息,说他们不能注册一个主题。
这是相关的表格
user - id, name, password
subject - id, name, user_id
lab - id, time, room, capacity, subject_id
user_lab - id, user_id, lab_id
我要写的是一个声明,如果用户尚未注册实验室,请打印subject.name, lab.time, lab.room
HttpSession session = request.getSession(true);
//Set data you want to send back to the request (will be forwarded to the page)
//Can set string, int, list, array etc.
String id = request.getParameter("id");
System.out.println("beej");
int user_id = 0;
if(id != null && (!id.equals("")) ) {
user_id= Integer.parseInt(id);
}
String sql = "SELECT l.id,s.name,l.day,l.time,l.room" +
" FROM subject s, lab l " +
" WHERE" +
" s.id = l.subject_id"+
" AND l.id "+"NOT IN"+
"SELECT l.id"+
" FROM lab ll, user_lab ul" +
"WHERE"+"l.id ="+"ul.lab_id" + "AND ul.user_id="+(Integer)session.getAttribute("id");
try{
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/wae","root","");
System.out.println(session.getAttribute("id"));
Statement stmt = con.createStatement();
ResultSet res = stmt.executeQuery(sql);
System.out.println(res);
ArrayList<String> list1 = new ArrayList<String>();
ArrayList<String> list2 = new ArrayList<String>();
if (res.next()){
do{
list1.add(res.getString(1));
list2.add(res.getString(2)+" "+res.getString(3)+" "+res.getString(4)+" "+res.getString(5));
}while(res.next());
String[] arr1 = list1.toArray(new String[list1.size()]);
String[] arr2 = list2.toArray(new String[list2.size()]);
System.out.println(list2);
request.setAttribute("res1", arr1);
request.setAttribute("res2", arr2);
request.setAttribute("user_id", user_id);
}
}catch (SQLException e) {
}
catch (Exception e) {
}
//Decides what page to send the request data to
RequestDispatcher view = request.getRequestDispatcher("Student_enrol.jsp");
//Forward to the page and pass the request and response information
view.forward(request, response);
org.apache.jasper.JasperException: java.lang.NullPointerException
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:534)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:457)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:391)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
Student_enrol.sendBack(Student_enrol.java:104)
Student_enrol.doGet(Student_enrol.java:40)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
root cause
java.lang.NullPointerException
org.apache.jsp.Student_005fenrol_jsp._jspService(Student_005fenrol_jsp.java:95)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:419)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:391)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
Student_enrol.sendBack(Student_enrol.java:104)
Student_enrol.doGet(Student_enrol.java:40)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)