[英]Null pointer exception while using jsp:usebean
I am getting a Null pointer exception while using the object created by <jsp:usebean>
. 使用
<jsp:usebean>
创建的对象时,出现Null指针异常。 The object has been properly instantiated, ( <jsp:getproperty>
is running fine ).But when I pass this created object in static method of Register
class,its throwing a Null Pointer Exception. 该对象已正确实例化(
<jsp:getproperty>
运行良好)。但是当我在Register
类的静态方法中传递此创建的对象时,它引发Null指针异常。
NewFile.htm 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 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 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 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 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;
}
} }
Here's your problem. 这是你的问题。
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/database","root","root");
} catch (Exception e) {
// TODO: handle exception
}
An Exception
is probably being thrown and you are swallowing it, ie. 一个
Exception
可能是被抛出,你是吞咽,即。 not handling it . 不处理 。
con
remains null
, but you return it. con
保留为null
,但您将其返回。
DBConnection.establishConnection()
therefore returns null
. 因此返回
null
。
You try to call 您尝试打电话
prepareStatement("insert into Person values (?,?,?)");
on null
. 在
null
。
The codes looks fine but thr may be two below reason to get null pointer exception
. 代码看起来不错,但是thr可能是以下两个导致
null pointer exception
原因。 As you are getting the connection
from method DBConnection.establishConnection()
so check this is providing proper and valid connection. 当您从方法
DBConnection.establishConnection()
获取connection
,请检查此连接是否提供正确且有效的连接。
The other thing can be Employee obj
, the obj
can be null
,as you are calling it from .jsp
so check it. 另一件事可以是
Employee obj
, obj
可以为null
,因为您是从.jsp
调用的,因此请检查它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.