Reputation: 467
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;
}
}
Upvotes: 0
Views: 1163
Reputation: 4923
The codes looks fine but thr may be two below reason to get null pointer exception
.
As you are getting the connection
from method DBConnection.establishConnection()
so check this is providing proper and valid connection.
The other thing can be Employee obj
, the obj
can be null
,as you are calling it from .jsp
so check it.
Upvotes: 0
Reputation: 279970
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. not handling it. con
remains null
, but you return it.
DBConnection.establishConnection()
therefore returns null
.
You try to call
prepareStatement("insert into Person values (?,?,?)");
on null
.
Upvotes: 1