【问题标题】:java null pointer exception on jsp mvc projectjsp mvc项目上的java空指针异常
【发布时间】:2018-05-19 20:07:05
【问题描述】:

我收到此错误 The server encountered an unexpected condition that prevented it from fulfilling the request. java.lang.NullPointerException。我正在尝试将数据插入 mysql 数据库。我创建了一个动态 Web 项目,在 index.jsp 文件中我有表单。我也在关注 MVC 架构。

这是我得到的错误

CustomerController.java serverlet

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    //doGet(request, response);

    //Get the data
    String name = request.getParameter("name");
    String email = request.getParameter("email");
    String address = request.getParameter("address");
    String sex = request.getParameter("sex");
    String bday = request.getParameter("bday");
    String tel = request.getParameter("tele");
    String password= request.getParameter("password");

    //Setup an Employee object
    Customer customer = new Customer();
    customer.setName(name);
    customer.setEmail(email);
    customer.setAddress(address);
    customer.setSex(sex);
    customer.setBday(bday);
    customer.setTelephone(tel);
    customer.setPassword(password);

    //Setup am EmpAddQuery object
    CustomerAddQuery obj = new CustomerAddQuery();

    //Pass the Employee to EmpAddQuery to add to the database
    obj.addData(customer); //line 69

    //Pass execution control to the ReadServlet
    String url = "/index.jsp";

    RequestDispatcher dispatcher = request.getRequestDispatcher(url);
    dispatcher.forward(request, response);

}

我只复制了servelet的doPost方法

CustomerAddQuery.java

package dbhelpers;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;

import models.Customer;

public class CustomerAddQuery {
    DBConnection databaseCon = new DBConnection();

    public void addData(Customer customer)
    {

        String query = "insert into customers (`name`, `email`, `address`, 
     `sex`, `bday`, `telephone` , `password`) values (?,?,?,?,?,?,?)" ;
        try {
            Connection con = databaseCon.dbconnect();
            PreparedStatement pst = con.prepareStatement(query); //line 27
            pst.setString(1, customer.getName());
            pst.setString(2, customer.getEmail());
            pst.setString(3, customer.getAddress());
            pst.setString(4, customer.getSex());
            pst.setString(5, customer.getBday());
            pst.setString(6, customer.getTelephone());
            pst.setString(7, customer.getPassword());
            pst.execute();


        } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

}

DBConnect.java 文件

package dbhelpers;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DBConnection {
   public static Connection dbconnect(){
        Connection con = null;
        try {

        Class.forName("com.mysql.jdbc.Driver");
        con = DriverManager.getConnection("jdbc:mysql://localhost/vehicle_rental_system", "root", "");


    }
    catch (ClassNotFoundException | SQLException e){
        System.out.println(e);
    }
       return con; 
   }
}

【问题讨论】:

  • 嗯,这是第 27 行?
  • 我已经在 CustomerAddQuery.java 文件代码部分注释了这一行
  • 你的连接是null
  • 是的,我正要说 dbconnect() 返回 null。请注意,您打印出异常,但不要抛出异常。检查服务器日志,我敢打赌那里有一个 ClassNotFoundException。这是一个很好的例子,说明为什么最好抛出异常而不是试图隐藏它们。
  • @J-Alex 我该如何解决这个问题?

标签: java jsp model-view-controller


【解决方案1】:

复制mysql jdbc驱动到Tomcat\lib目录解决

【讨论】:

    猜你喜欢
    • 2020-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多