使用jsp:usebean时出现空指针异常

时间:2014-02-02 17:39:50

标签: java jsp

使用<jsp:usebean>创建的对象时,我收到Null指针异常。该对象已被正确实例化,(<jsp:getproperty>运行正常)。但是当我在Register类的静态方法中传递此创建的对象时,它会抛出一个空指针异常。

NewFile.htm

    <!DOCTYPE html>
<html>
<form action="NewFile.jsp">
Name<input type="text" name="textbox1"/>
<br>
ID<input type="text" name="textbox2"/>
<br><select name="select1">
<option>India</option>
<option>France</option>
<option>Japan</option>

</select>
<br>
<input type="submit"/>

</form>
</body>
</html>

NewFile.jsp

<%@ page import="com.psl.Register" %>
<jsp:useBean id="emp" class="com.psl.Employee"/>
<jsp:setProperty property="name" name="emp" param="textbox1"/>
<jsp:setProperty property="country" name="emp" param="select1"/>
<jsp:setProperty property="id" name="emp" param="textbox2"/>
<jsp:getProperty property="country" name="emp"/> 
<%Register.register(emp);%>

Register.java

package com.psl;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class Register {
    public static void register (Employee obj)
    {

        try {
            PreparedStatement stm = DBConnection.establishConnection().prepareStatement("insert into Person values (?,?,?)");
            stm.setString(1,obj.getName() );
            stm.setInt(2, obj.getId());
            stm.setString(3, obj.getCountry());
    stm.execute();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}

Employee.java

package com.psl;

import java.util.Arrays;

public class Employee {



    @Override
    public String toString() {
        return "Employee [name=" + name + ", id=" + id + ", country=" + country
                + "]";
    }

    private String name;
    private int id;
    private String country;


    public Employee() {
        // TODO Auto-generated constructor stub
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }


}

DBConnection.java

public class DBConnection {

private static Connection con ;
public static Connection establishConnection()
{try {

    Class.forName("com.mysql.jdbc.Driver");
    con=DriverManager.getConnection("jdbc:mysql://localhost:3306/database","root","root");
    } catch (Exception e) {
        // TODO: handle exception
    }   
return con;

}

}

2 个答案:

答案 0 :(得分:1)

这是你的问题。

Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/database","root","root");
} catch (Exception e) {
    // TODO: handle exception
}  

Exception可能被扔了,你正吞下它,即。 不处理con仍为null,但您将其退回。

DBConnection.establishConnection()

因此返回null

你试着打电话

prepareStatement("insert into Person values (?,?,?)");

on null


Don't use scriptlets.

答案 1 :(得分:0)

代码看起来很好但是获得null pointer exception可能有两个原因。 当您从方法connection获取DBConnection.establishConnection()时,请检查这是否提供了正确且有效的连接。

另一件事可以是Employee objobj可以是null,因为你是从.jsp调用的,所以请检查一下。