исключение нулевого указателя java в проекте jsp mvc - PullRequest
0 голосов
/ 19 мая 2018

Я получаю эту ошибку The server encountered an unexpected condition that prevented it from fulfilling the request. java.lang.NullPointerException.Я пытаюсь вставить данные в базу данных MySQL.Я создал динамический веб-проект, и в файле index.jsp у меня есть форма.Также я следую архитектуре MVC.

Это ошибка, которую я получаю

error

CustomerController.java servelet

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

}

Я только скопировал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 file

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

1 Ответ

0 голосов
/ 19 мая 2018

Решено путем копирования драйвера mysql jdbc в каталог Tomcat \ lib

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...