服务器遇到内部错误,导致无法完成此请求。显示java.lang.NullPointerException

时间:2015-10-25 02:54:32

标签: java jsp servlets model-view-controller jdbc

这里只是一个新手。我刚刚使用MVC框架(JSP和Servlets)创建了一个简单的注册页面,并在数据库中注册它,当我试图运行它时,我收到了一个错误

Exception report:
    SEVERE: Servlet.service() for servlet [Controller.RegistrationServlet] in context with path [/RegistrationApp] threw exception
    java.lang.NullPointerException
        at ModelDb.ModelDBConnect.registerUser(ModelDBConnect.java:55)
        at Controller.RegistrationServlet.doPost(RegistrationServlet.java:63)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)

这是我的源代码:

Registrationview.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Registration Form</title>
</head>
<style>
body {background-color: #101010 }
h3 {color:white}
td{color: white}
</style>
<body>
<h3>Registration Page</h3>
<br>
<form action="Registration" method="post">
<table>
<tr>
<td>Employee Id:</td>
<td><input type="text" name="EMPLOYEE_ID"> Job Id:</td><td><input type="text" name="JOB_ID"></td>
<tr>
<td>First Name:</td>
<td><input type="text" name="FIRST_NAME"> Salary:</td><td><input type="text" name="SALARY"></td>
</tr>
<tr>
<td>Last Name:</td>
<td><input type="text" name="LAST_NAME"> Commission:</td><td><input type="text" name="COMMISSION_PCT"></td>
</tr>
<tr>
<td>Email:</td>
<td><input type="text" name="EMAIL"> Manager Id:</td><td><input type="text" name="MANAGER_ID"></td>
</tr>
<tr>
<td>Phone Number:</td>
<td><input type="text" name="PHONE_NUMBER"> Department Id:</td><td><input type="text" name="DEPARTMENT_ID"></td>
</tr>
<tr>
<td>Hire Date:</td>
<td><input type="text" name="HIRE_DATE">
</tr>
</table><br>
<input type="submit" value="Register">
</form>

</body>
</html>

控制器:

RegistrationServlet

package Controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Model.RegistrationBean;
import ModelDb.ModelDBConnect;


@WebServlet("/Registration")
public class RegistrationServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;


    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        // 1) Retrieve all parameters from JSP Page

        String EMPLOYEE_ID = request.getParameter("EMPLOYEE_ID");
        String FIRST_NAME = request.getParameter("FIRST_NAME");
        String LAST_NAME = request.getParameter("LAST_NAME");
        String EMAIL= request.getParameter("EMAIL");
        String PHONE_NUMBER = request.getParameter("PHONE_NUMBER");
        String HIRE_DATE = request.getParameter("HIRE_DATE");
        String JOB_ID = request.getParameter("JOB_ID");
        String SALARY = request.getParameter("SALARY");
        String COMMISSION_PCT = request.getParameter("COMMISSION_PCT");
        String MANAGER_ID = request.getParameter("MANAGER_ID");
        String DEPARTMENT_ID = request.getParameter("DEPARTMENT_ID");

        //2) set all the values in the model class object

        RegistrationBean rb = new RegistrationBean();

        rb.setEMPLOYEE_ID(EMPLOYEE_ID);
        rb.setFIRST_NAME(FIRST_NAME);
        rb.setLAST_NAME(LAST_NAME);
        rb.setEMAIL(EMAIL);
        rb.setPHONE_NUMBER(PHONE_NUMBER);
        rb.setHIRE_DATE(HIRE_DATE);
        rb.setJOB_ID(JOB_ID);
        rb.setSALARY(SALARY);
        rb.setCOMMISSION_PCT(COMMISSION_PCT);
        rb.setMANAGER_ID(MANAGER_ID);
        rb.setDEPARTMENT_ID(DEPARTMENT_ID);

        // 3) Call a method from ModelDBConnect class to insert data in the table


        String sql ="Insert into EMPLOYEES" + "(EMPLOYEE_ID, FIRST_NAME, LAST_NAME, EMAIL, PHONE_NUMBER, HIRE_DATE, JOB_ID, SALARY, COMMISSION_PCT, MANAGER_ID, DEPARTMENT_ID) VALUES" +
        "(?,?,?,?,?,?,?,?,?,?,?)";

        int i = ModelDBConnect.registerUser(rb, sql);

        if (i != 0) {
            System.out.println("value inserted");
        }
        else {
            System.out.println("value not inserted");
        }


    }

}

的JavaBean

RegistrationBean

package Model;

public class RegistrationBean {

    private String EMPLOYEE_ID;
    private String FIRST_NAME;
    private String LAST_NAME;
    private String EMAIL;
    private String PHONE_NUMBER;
    private String HIRE_DATE;
    private String JOB_ID;
    private String SALARY;
    private String COMMISSION_PCT;
    private String MANAGER_ID;
    private String DEPARTMENT_ID;

    public String getEMPLOYEE_ID() {
        return EMPLOYEE_ID;
    }
    public void setEMPLOYEE_ID(String eMPLOYEE_ID) {
        EMPLOYEE_ID = eMPLOYEE_ID;
    }
    public String getFIRST_NAME() {
        return FIRST_NAME;
    }
    public void setFIRST_NAME(String fIRST_NAME) {
        FIRST_NAME = fIRST_NAME;
    }
    public String getLAST_NAME() {
        return LAST_NAME;
    }
    public void setLAST_NAME(String lAST_NAME) {
        LAST_NAME = lAST_NAME;
    }
    public String getEMAIL() {
        return EMAIL;
    }
    public void setEMAIL(String eMAIL) {
        EMAIL = eMAIL;
    }
    public String getPHONE_NUMBER() {
        return PHONE_NUMBER;
    }
    public void setPHONE_NUMBER(String pHONE_NUMBER) {
        PHONE_NUMBER = pHONE_NUMBER;
    }
    public String getHIRE_DATE() {
        return HIRE_DATE;
    }
    public void setHIRE_DATE(String hIRE_DATE) {
        HIRE_DATE = hIRE_DATE;
    }
    public String getJOB_ID() {
        return JOB_ID;
    }
    public void setJOB_ID(String jOB_ID) {
        JOB_ID = jOB_ID;
    }
    public String getSALARY() {
        return SALARY;
    }
    public void setSALARY(String sALARY) {
        SALARY = sALARY;
    }
    public String getCOMMISSION_PCT() {
        return COMMISSION_PCT;
    }
    public void setCOMMISSION_PCT(String cOMMISSION_PCT) {
        COMMISSION_PCT = cOMMISSION_PCT;
    }
    public String getMANAGER_ID() {
        return MANAGER_ID;
    }
    public void setMANAGER_ID(String mANAGER_ID) {
        MANAGER_ID = mANAGER_ID;
    }
    public String getDEPARTMENT_ID() {
        return DEPARTMENT_ID;
    }
    public void setDEPARTMENT_ID(String dEPARTMENT_ID) {
        DEPARTMENT_ID = dEPARTMENT_ID;
    }

}

连接数据库:

ModelDBConnect.java

package ModelDb;

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

import Model.RegistrationBean;

public class ModelDBConnect {

    // JDBC driver name and database

    static final String JDBC_Driver = "oracle.jdbc.OracleDriver";
    static final String DB_URL = "jdbc:oracle:thin:@//localhost:1521/XE";

    // Database credentials

    static final String USERNAME = "hr";
    static final String PASSWORD = "hr1";

    // method to connect to the database

    private static Connection connect() {

        Connection con = null;

        // Register the JDBC Driver

        try {
            Class.forName("oracle.jdbc.OracleDriver");

            // open a connection
            System.out.println("Connection to the database");

            con=DriverManager.getConnection(DB_URL, USERNAME, PASSWORD);

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


        return con;
    }

    public static int registerUser(RegistrationBean rb, String sql) {
        int i = 0;

        Connection con = connect();
        try {
            PreparedStatement ps =con.prepareStatement(sql);-------Error here!
            ps.setString(1, rb.getEMPLOYEE_ID());
            ps.setString(2, rb.getFIRST_NAME());
            ps.setString(3, rb.getLAST_NAME());
            ps.setString(4, rb.getEMAIL());
            ps.setString(5, rb.getPHONE_NUMBER());
            ps.setString(6, rb.getHIRE_DATE());
            ps.setString(7, rb.getJOB_ID());
            ps.setString(8, rb.getSALARY());
            ps.setString(9, rb.getCOMMISSION_PCT());
            ps.setString(10, rb.getMANAGER_ID());
            ps.setString(11, rb.getDEPARTMENT_ID());

            i=ps.executeUpdate();

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


        return i;

    }


}

谢谢!

1 个答案:

答案 0 :(得分:1)

由于您的Connection con = connect();方法失败,我猜您的connect()被指定为空。

不是在失败时抛出异常,而是简单地打印堆栈跟踪 - 然后继续,好像什么也没发生。 (“好的,现在怎样?”)

请查找并查看生成的堆栈跟踪,以获取有关连接失败原因的其他一些线索。它可能是无效的连接字符串,用户名/密码或本地数据库本身的问题。

你最好重新抛出异常,或者根本不抓住它,并声明它可以作为方法的一部分抛出。然后,您可以依赖connect()返回有效连接 - 或者以异常方式失败。

按原样,在尝试使用它之前,首先需要检查con是否为null。当然,尝试在.prepareStatement上调用con时抛出NullPointerException,这是空的。