0

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;

}

}

Sanjana
  • 467
  • 2
  • 8
  • 20

2 Answers2

1

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.


Don't use scriptlets.

Community
  • 1
  • 1
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • Yup I have included the printstacktrace and I could see the exception now.The sql jar file was not placed in web-inf due to which I was getting an exception. – Sanjana Feb 02 '14 at 18:10
  • @Sanjana Always take care in handling the exceptions explicitly. Your handling doesn't have to be intense, but at least logging the exception will go a long way. – Sotirios Delimanolis Feb 02 '14 at 18:10
0

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.

Kick
  • 4,823
  • 3
  • 22
  • 29