0

我正在创建一个执行 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)
4

3 回答 3

0

NOT IN在和之间添加一个空格SELECT

" s.id = l.subject_id"+
" AND l.id "+"NOT IN  " +     // <== here
"SELECT l.id"+

你的子查询应该是这样的

SELECT..
FROM...
WHERE l.id NOT IN
(
   SELECT..
   FROM...
)
于 2012-10-08T07:52:06.357 回答
0

您还需要在“WHERE”周围和“AND”之前使用一些空格

"WHERE"+"l.id ="+"ul.lab_id" + "AND ul.user_id="+(Integer)session.getAttribute("id"); 

应该

" WHERE "+"l.id ="+"ul.lab_id" + " AND ul.user_id="+(Integer)session.getAttribute("id"); 
于 2012-10-08T08:43:37.877 回答
0

对您的代码进行以下更改:

  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);

至:

  String[] arr1 = null;
  String[] arr2 = null;
  while(res.next()){
       list1.add(res.getString(1));
       list2.add(res.getString(2)+" "+res.getString(3)+" "+res.getString(4)+" "+res.getString(5));
      arr1 = list1.toArray(new String[list1.size()]);
      arr2 = list2.toArray(new String[list2.size()]);
          }
  request.setAttribute("res1", arr1);
  request.setAttribute("res2", arr2);

这是因为当您尝试打印一个空的 list2 或访问 list1 和 list2 时,它会抛出错误。为了避免这种情况,仅在行存在时才使用它们。

如 John Woo 所述,还可以在查询中的单词之间添加空格 :)

于 2015-06-24T10:37:42.447 回答