MVC结构化程序的java.lang.NullPointerException

时间:2014-03-15 12:58:25

标签: java jsp exception servlets javabeans

每当我尝试在项目中运行特定的jsp时,我都会收到此错误

enter image description here

我已经完成了我能想到的一切(包括重新创建它需要的数据库,检查并重新检查我认为有错误的行,Projectbean.java的第165行,更具体地说是准备好的语句部分公共int rowCount()功能) 如果有人能指出我正确的方向,我们将不胜感激。

Projectservlet.java:

    import project.Projectbean;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.sql.SQLException;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.servlet.RequestDispatcher;
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    /**
     *
     * @author REY
     */
    @WebServlet(name = "Projectservlet", urlPatterns = {"/Projectservlet"})
    public class Projectservlet extends HttpServlet {

        protected void processRequest(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            response.setContentType("text/html;charset=UTF-8");
            PrintWriter out = response.getWriter();
            try {
                /* TODO output your page here. You may use following sample code. */
                out.println("<html>");
                out.println("<head>");
                out.println("<title>Servlet Projectservlet</title>");            
                out.println("</head>");
                out.println("<body>");
                out.println("<h1>Servlet Projectservlet at " + request.getContextPath() + "</h1>");
                out.println("</body>");
                out.println("</html>");
            } finally {            
                out.close();
            }
        }

        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
         String option=request.getParameter("menu");

            if(option == null || option.equals("menu"))
            {   
                RequestDispatcher dr = request.getRequestDispatcher("Menu.jsp");
                dr.forward(request, response);
            }
            else if(option.equals("stud"))
            {
                RequestDispatcher dr = request.getRequestDispatcher("Student.jsp");
                dr.forward(request, response);   
            }
            else if(option.equals("book"))
            {
                RequestDispatcher dr = request.getRequestDispatcher("Book.jsp");
                dr.forward(request, response);   
            }
            else if(option.equals("cat"))
            {
                RequestDispatcher dr = request.getRequestDispatcher("Category.jsp");
                dr.forward(request, response);   
            }
            else if(option.equals("Borrow"))
            {
                RequestDispatcher dr = request.getRequestDispatcher("Borrow.jsp");
                dr.forward(request, response);   
            }
            else if(option.equals("Return"))
            {
                RequestDispatcher dr = request.getRequestDispatcher("Return.jsp");
                dr.forward(request, response);   
            }   

        }

        @Override
        protected void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            PrintWriter out = response.getWriter();
            String option = request.getParameter("option");

     //STUDENT CONTROLLER
            if (option.equals("Add Student"))
            {
                 RequestDispatcher dr = request.getRequestDispatcher("AddStudent.jsp");
                 dr.include(request, response); 
            }
            else if (option.equals("addedS"))
            {
                    String id = request.getParameter("Studid");

                    int a = Integer.parseInt(id);

                    Projectbean addCustomer = new Projectbean();
                    addCustomer.setStudid(a);
                    addCustomer.setFname(request.getParameter("Fname"));
                    addCustomer.setLname(request.getParameter("Lname"));
                    addCustomer.setCourse(request.getParameter("Course"));

                   addCustomer.AddStudent();
                   RequestDispatcher dr = request.getRequestDispatcher("AddStudtConfirm.jsp");
                   dr.include(request, response);

            }

            else if (option.equals("Edit Student"))
            {
                RequestDispatcher dr = request.getRequestDispatcher("EditCustomer.jsp");
                dr.include(request, response); 
            }
            else if (option.equals("editS"))
            {
                String Studid = request.getParameter("edit");  
                request.setAttribute("Studid", Studid);

                RequestDispatcher dr = request.getRequestDispatcher("EditedStudent.jsp");
                dr.include(request, response); 
            }
            else if (option.equals("editedC"))
            {
                Projectbean edit = new Projectbean();
                int id = Integer.parseInt(request.getParameter("Studid"));
                edit.setStudid(id);
                edit.setFname(request.getParameter("Fname"));
                edit.setLname(request.getParameter("Lname"));
                edit.setCourse(request.getParameter("Course"));
                edit.EditStudent();
                RequestDispatcher dr = request.getRequestDispatcher("EditStudConfirm.jsp");
                dr.include(request, response);
            }
            else if (option.equals("Delete Student"))
            {
                RequestDispatcher dr = request.getRequestDispatcher("DeleteStudent.jsp");
                dr.include(request, response); 
            }
            else if (option.equals("deleteS"))
            {
                String ID = request.getParameter("delete");
                int custID = Integer.parseInt(ID);
                Projectbean delete = new Projectbean();
                delete.DeleteStudent(custID);

                RequestDispatcher dr = request.getRequestDispatcher("DeleteStudConfirm.jsp");
                dr.include(request, response);  
            }
            else if (option.equals("Search Student"))
            {
                RequestDispatcher dr = request.getRequestDispatcher("SearchStudent.jsp");
                dr.include(request, response);
            }
            else if (option.equals("searchS"))
            {
                String search = request.getParameter("search");  
                request.setAttribute("search", search);
                RequestDispatcher dr = request.getRequestDispatcher("SearchedStudent.jsp");
                dr.include(request, response);

            }
    //CATEGORY CONTROLLER
            else  if (option.equals("Add Category"))
            {
                 RequestDispatcher dr = request.getRequestDispatcher("AddCategory.jsp");
                 dr.include(request, response); 
            }
            else if (option.equals("addedCat"))
            {
                    String id = request.getParameter("Catid");

                    int a = Integer.parseInt(id);

                    Projectbean addCategory = new Projectbean();
                    addCategory.setCatid(a);
                    addCategory.setCatname(request.getParameter("Catname"));

                   addCategory.AddCategory();
                   RequestDispatcher dr = request.getRequestDispatcher("AddCatConfirm.jsp");
                   dr.include(request, response);
            }

            else if (option.equals("Edit Category"))
            {
                RequestDispatcher dr = request.getRequestDispatcher("EditCategory.jsp");
                dr.include(request, response); 
            }
            else if (option.equals("editCat"))
            {

                String catID = request.getParameter("catedit");  
                request.setAttribute("Catid", catID);
                out.println (catID);
                RequestDispatcher dr = request.getRequestDispatcher("EditedCategory.jsp");
                 dr.forward(request, response); 

            }
            else if (option.equals("editedCat"))
            {
                Projectbean editcat = new Projectbean();
                int id = Integer.parseInt(request.getParameter("Catid"));
                editcat.setCatid(id);
                editcat.setCatname(request.getParameter("Catname"));
                editcat.EditCategory();
                RequestDispatcher dr = request.getRequestDispatcher("EditCatConfirm.jsp");
                dr.include(request, response);
            }

            else if (option.equals("Delete Category"))
            {
                RequestDispatcher dr = request.getRequestDispatcher("DeleteCategory.jsp");
                dr.include(request, response); 
            }
            else if (option.equals("delCat"))
            {
                String ID = request.getParameter("catdelete");
                int Catid = Integer.parseInt(ID);
                Projectbean catdelete = new Projectbean();
                catdelete.DeleteCategory(Catid);

                RequestDispatcher dr = request.getRequestDispatcher("DeleteCatConfirm.jsp");
                dr.include(request, response);  
            }

             else if (option.equals("Search Category"))
            {
                RequestDispatcher dr = request.getRequestDispatcher("SearchCategory.jsp");
                dr.include(request, response);
            }
            else if (option.equals("searchCat"))
            {
                String search = request.getParameter("search");  
                request.setAttribute("search", search);
                RequestDispatcher dr = request.getRequestDispatcher("SearchedCategory.jsp");
                dr.include(request, response);

            }

    //PRODUCT CONTROLLER
            else  if (option.equals("Add Book"))
            {
                 RequestDispatcher dr = request.getRequestDispatcher("AddBook.jsp");
                 dr.include(request, response); 
            }
            else if (option.equals("addedBook"))
            {
                    String id = request.getParameter("Bookid");
                    String Catid = request.getParameter("edit");
                    int catID = Integer.parseInt (Catid);
                    int Bookid = Integer.parseInt(id);
                    //int numavail = Integer.parseInt(request.getParameter("availNo"));
                    //double price = Double.parseDouble(request.getParameter("prodPrice"));

                    Projectbean addProduct = new Projectbean();
                    addProduct.setBookid(Bookid);
                    addProduct.setTitle(request.getParameter("Title"));
                    addProduct.setAuthor(request.getParameter("Author"));
                    //addProduct.setProdAvail(numavail);
                    //addProduct.setProdPrice(price);
                    addProduct.setCatid(catID);
                    addProduct.AddBook();

                    RequestDispatcher dr = request.getRequestDispatcher("AddBookConfirm.jsp");
                    dr.include(request, response);
            }

            else if (option.equals("Edit Book"))
            {
                RequestDispatcher dr = request.getRequestDispatcher("EditBook.jsp");
                dr.include(request, response); 
            }
            else if (option.equals("editBook"))
            {

                String Bookid = request.getParameter("prodedit");  
                request.setAttribute("Bookid", Bookid);
                out.println (Bookid);
                RequestDispatcher dr = request.getRequestDispatcher("EditedBook.jsp");
                 dr.forward(request, response); 

            }
            else if (option.equals("editedBook"))
            {
                Projectbean editprod = new Projectbean();
                int id = Integer.parseInt(request.getParameter("Bookid"));
                //int avail = Integer.parseInt (request.getParameter("prodavail"));
                double price = Double.parseDouble(request.getParameter("prodprice"));
                int catID = Integer.parseInt(request.getParameter("category"));

                editprod.setBookid(id);
                editprod.setTitle(request.getParameter("Title"));
                editprod.setAuthor(request.getParameter("Author"));
                //editprod.setProdAvail(avail);
                //editprod.setProdPrice(price);
                editprod.setCatid(catID);
                editprod.EditBook();
                RequestDispatcher dr = request.getRequestDispatcher("EditBookConfirm.jsp");
                dr.include(request, response);
            }

            else if (option.equals("Delete Book"))
            {
                RequestDispatcher dr = request.getRequestDispatcher("DeleteBook.jsp");
                dr.include(request, response); 
            }
            else if (option.equals("delBook"))
            {
                String ID = request.getParameter("bookdel");
                int prodID = Integer.parseInt(ID);
                Projectbean catdelete = new Projectbean();
                catdelete.DeleteBook(prodID);

                RequestDispatcher dr = request.getRequestDispatcher("DeleteBookConfirm.jsp");
                dr.include(request, response);  
            }

             else if (option.equals("Search Book"))
            {
                RequestDispatcher dr = request.getRequestDispatcher("SearchBook.jsp");
                dr.include(request, response);
            }
            else if (option.equals("searchBook"))
            {
                String id = request.getParameter("category");
                String search = request.getParameter("search");
                request.setAttribute("Catid",id);
                request.setAttribute("search", search);
                RequestDispatcher dr = request.getRequestDispatcher("SearchedBook.jsp");
                dr.include(request, response);

            }
        }
        @Override
        public String getServletInfo() {
            return "Short description";
        }

    }

Projectbean.java

package project;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Projectbean {
    private Connection con;
    //student
    int Studid;
    String Fname;
    String Lname;
    String Course;

    //book
    int Bookid; 
    String Title;
    String Author;
    String Dborrowed;
    String Dreturned;
    int Fee;

    //Category
    int Catid;
    String Catname;

    //student setters
    public void setStudid (int id)
    {
        Studid = id;
    }
    public void setFname (String val)
    {
        Fname = val;
    }
    public void setLname (String val)
    {
        Fname = val;
    }
    public void setCourse (String val)
    {
        Course = val;
    }
    //student getters
    public int getStudid ()
    {
        return Studid ;
    }
    public String getFname ()
    {
        return Fname ;
    }
    public String getLname ()
    {
        return Lname ;
    }
    public String getCourse ()
    {
        return Course ;
    }

    //book setters
    public void setBookid (int id)
    {
        Bookid = id;
    }
    public void setTitle (String val)
    {
        Title = val;
    }
    public void setAuthor (String val)
    {
        Author = val;
    }
    public void setDborrowed (String val)
    {
        Dborrowed = val;
    }
    public void setDreturned (String val)
    {
        Dreturned = val;
    }
    public void setFee (int fee)
    {
        Fee = fee;
    }
    //book getters
    public int getBookid ()
    {
        return Bookid ;
    }
    public String getTitle ()
    {
        return Title ;
    }
    public String getAuthor ()
    {
        return Author ;
    }
    public String getDborrowed ()
    {
        return Dborrowed ;
    }
    public String getDreturned ()
    {
        return Dreturned ;
    }
    public int getFee ()
    {
        return Fee ;
    }

    //Category setters
    public void setCatid (int id)
    {
        Catid = id ;
    }
    public void setCatname(String val)
    {
        Catname = val;
    }
    //Category getters
    public int getCatid()
    {
        return Catid;
    }
    public String getCatname()
    {
        return Catname;
    }


    public Projectbean(){
        try{
            Class.forName("org.apache.derby.jdbc.ClientDriver");
            con = DriverManager.getConnection(
                "jdbc:derby://localhost:1527/final",
                "", "");
        }catch(Exception e){
            System.out.println("Exception" + e);
        }
    }

    //student
    public ResultSet GetStudent() throws SQLException, Exception{
        Statement s = con.createStatement();
        ResultSet rs = s.executeQuery("Select * from APP.STUDENT");
        return rs;
    }
    //set student id
    public int rowCount() throws SQLException{
           PreparedStatement st= con.prepareStatement("SELECT STUDENTID FROM APP.STUDENT");
           ResultSet rs = st.executeQuery();
           //get the number of rows from the result set
           int count = 0;
           while (rs.next()){
           count = rs.getInt(1);
           }
           return count;
    }
    //get specific student
    public void GetSpecificStudent (String StudID) throws SQLException, Exception{
        int id = Integer.parseInt(StudID);
        Statement s = con.createStatement();
        ResultSet rs = s.executeQuery("Select * from APP.STUDENT WHERE STUDID="+id+" ");
        rs.next();
        this.Studid = id;
        Fname = rs.getString("FIRSTNAME");
        Lname = rs.getString ("LASTNAME");
        Course = rs.getString("COURSE");
    }
    //Add Student
    public void AddStudent(){
        String read = "insert into APP.STUDENT (STUDID,FIRSTNAME,LASTNAME,COURSE) values(?,?,?,?)";
        PreparedStatement addrec;
        try {
        addrec = con.prepareStatement(read);
        addrec.setInt(1, Studid);
        addrec.setString(2, Fname);
        addrec.setString(3, Lname);
        addrec.setString(4, Course);
        addrec.execute();
        }
        catch (SQLException e){
            System.out.println ("Exception " + e);
        }
    }
    //edit student
    public void EditStudent(){
        String read = "Update APP.STUDENT set STUDID=?, FIRSTNAME=?, LASTNAME=?, COURSE=? where ID="+Studid;
        PreparedStatement editRec;
        try{
                editRec = con.prepareStatement(read);
        editRec.setInt (1,Studid);
                editRec.setString(2,Fname);
        editRec.setString(3,Lname);
                editRec.setString(4,Course);
                editRec.execute();
        }
        catch (SQLException e){
            System.out.println ("Exception " + e);
            }
    }
    //Delete student
    public void DeleteStudent (int id) {
    try
        { 
            Statement s = con.createStatement(); 
            String  read= "Delete FROM APP.STUDENT where STUDID = "+id+"";
            s.executeUpdate(read);
            }
            catch(SQLException e)
            {
                System.out.println(e);
            }    
    }
    //search student
    public ResultSet SearchStudent(String search) throws SQLException, Exception{
        String[] split = search.split("\\s+");
        int count = split.length;
        Statement s = con.createStatement();
        ResultSet rs = s.executeQuery("Select * from APP.STUDENT WHERE FIRSTNAME= '"+split[0]+"' OR LASTNAME= '"+split[count-1]+"' ");
        return rs;
    }

    //BOOK
    public ResultSet GetBook() throws SQLException, Exception{
        Statement s = con.createStatement();
        ResultSet rs = s.executeQuery("Select * from APP.BOOK");
        return rs;
    }
    //set book id
    public int BookrowCount() throws SQLException{
        PreparedStatement st= con.prepareStatement("SELECT BOOKID FROM APP.BOOK");
           ResultSet rs = st.executeQuery();
           //get the number of rows from the result set
           int count = 0;
           while (rs.next()){
           count = rs.getInt(1);
           }
           return count;
    }
    //get specific book
    public void GetSpecificBook (String BookID) throws SQLException, Exception{
        int id = Integer.parseInt(BookID);
        Statement s = con.createStatement();
        ResultSet rs = s.executeQuery("Select * from APP.BOOK WHERE custID="+id+" ");
        rs.next();
        this.Bookid = id;
        Title = rs.getString("TITLE");
        Author = rs.getString ("AUTHOR");
        Dborrowed = rs.getString("DATEBORROWED");
        Dreturned = rs.getString("DATERETURNED");
        Fee = rs.getInt("BORROWERSFEE");
    }
    //Add book
    public void AddBook(){
        String read = "insert into APP.BOOK (BOOKID,TITLE,AUTHOR,CATEGORYID) values(?,?,?)";
        PreparedStatement addrec;
        try {
        addrec = con.prepareStatement(read);
        addrec.setInt(1, Bookid);
        addrec.setString(2, Title);
        addrec.setString(3, Author);
        addrec.setInt(4, Catid);
        addrec.execute();
        }
        catch (SQLException e){
            System.out.println ("Exception " + e);
        }
    }
    //edit book
    public void EditBook(){
        String read = "Update APP.BOOK set BOOKID=?, TITLE=?, AUTHOR=?, CATEGORYID=? where BOOKID="+Bookid;
        PreparedStatement editRec;
        try{
                editRec = con.prepareStatement(read);
        editRec.setInt (1,Bookid);
                editRec.setString(2,Title);
        editRec.setString(3,Author);
                editRec.setInt(4,Catid);
                editRec.execute();
        }
        catch (SQLException e){
            System.out.println ("Exception " + e);
            }
    }
    //delete book
    public void DeleteBook (int id) {
    try
        { 
            Statement s = con.createStatement(); 
            String  read= "Delete FROM APP.BOOK where BOOKID = "+id+"";
            s.executeUpdate(read);
            }
            catch(SQLException e)
            {
                System.out.println(e);
            }    
    }
    //search book
    public ResultSet SearchBook(String search) throws SQLException, Exception{
        String[] split = search.split("\\s+");
        int count = split.length;
        Statement s = con.createStatement();
        ResultSet rs = s.executeQuery("Select * from APP.book WHERE TITLE= '"+split[0]+"' OR AUTHOR= '"+split[count-1]+"' ");
        return rs;
    }

    //CATEGORY
    //Set Category ID
    public int rowCountCat() throws SQLException{
        PreparedStatement st= con.prepareStatement("SELECT CATEGORYID FROM APP.CATEGORY");
           ResultSet rs = st.executeQuery();
           //get the number of rows from the result set
           int count1 = 0;
           while (rs.next()){
           count1 = rs.getInt(1);
           }
           return count1;
    }
    //Add Category
    public void AddCategory (){
        String read = "insert into APP.CATEGORY (CATEGORYID,CATEGORYNAME) values(?,?)";
        PreparedStatement addrec;
        try {
            addrec = con.prepareStatement(read);
            addrec.setInt(1, Catid);
            addrec.setString(2, Catname);
            addrec.execute();
        }
        catch (SQLException e){
            System.out.println ("Exception " + e);
        }
    }
    //Get List of Category
    public ResultSet GetCategory () throws SQLException, Exception{
        Statement s = con.createStatement();
        ResultSet rs = s.executeQuery("Select * from APP.CATEGORY");
        return rs;
    }
    //Get Specific Category
    public void GetSpecificCategory (String catID) throws SQLException, Exception {
        int id = Integer.parseInt(catID);
        Statement s = con.createStatement();
        ResultSet rs = s.executeQuery("Select * from APP.CATEGORY WHERE CATEGORYID="+id+" ");
        rs.next();
        this.Catid = id;
        Catname = rs.getString("CATEGORYNAME");
    }
    //EDIT Category
    public void EditCategory () {
        String read = "Update APP.CATEGORY set CATEGORYID=?, CATEGORYNAME=? WHERE CATEGORYID="+Catid;
        PreparedStatement editRec;
        try{
                editRec = con.prepareStatement(read);
        editRec.setInt (1,Catid);
                editRec.setString(2,Catname);
                editRec.execute();
        }
        catch (SQLException e){
            System.out.println ("Exception " + e);
        }
    }
    //Delete Category
    public void DeleteCategory (int id) {
    try
        { 
            Statement s = con.createStatement(); 
            String  read= "Delete FROM APP.CATEGORY where CATEGORYID = "+id+"";
            s.executeUpdate(read);
            }
            catch(SQLException e)
            {
                System.out.println(e);
            }    
    }
    //Search Category
    public ResultSet SearchCategory (String search) throws SQLException, Exception{
        String[] split = search.split("\\s+");
        int count = split.length;
        Statement s = con.createStatement();
        ResultSet rs = s.executeQuery("Select * from APP.CATEGORY WHERE CATEGORYNAME= '"+split[0]+"' OR CATEGORYNAME= '"+split[count-1]+"' ");
        return rs;   
    }
}

1 个答案:

答案 0 :(得分:0)

此时

con可能主要是null,因为此时没有其他候选人可以null