I am getting a Null pointer exception while using the object created by <jsp:usebean>
. 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.
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;
}
}