0

Image of step into for saveEmployee method:

enter image description hereI am learning Spring+JDBC, and trying to get SQL date-datatype into Java as java.util.Date or java.sql.Date object, and running into null pointer exception.

Code in the main method for inserting data into Employeetabke that causes exception:

//the following parses date string into java.util.date
    ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");
    EmployeeDAO empDao = (EmployeeDAO)ctx.getBean("employeeDao");
    Employee employee = new Employee();
    employee.setName("Robert");
    //the following parses date string into java.util.date
    employee.setJoining_Date((new SimpleDateFormat("MM/dd/yyyy")).parse("12/30/2016"));
    employee.setSalary(200);
    employee.setsSN("837463");

Definition of the setters in Employee.java model class:

import java.sql.Date

public void setJoining_Date(java.util.Date joining_Date) {
        this.joining_Date = new java.sql.Date(joining_Date.getTime());
}
public void setJoining_Date(Date joining_Date){
        this.joining_Date = joining_Date;
}

Method:

public void saveEmployee(Employee e){
    this.jdbcTemplate.update(this.employee_insert, new Object[]{e.getName(), e.getJoining_Date(), e.getSalary(), e.getsSN()});
    }

Exception:

Exception in thread "main" java.lang.NullPointerException
    at com.springdao.EmployeeDaoImpl.saveEmployee(EmployeeDaoImpl.java:49)
    at com.springclient.EmployeeExec.main(EmployeeExec.java:28)

Please also advise about best practices regarding implementing such code.

Spring.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
            xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation ="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-   
            2.0.xsd">

    <bean id ="dataSource" class        
      ="org.springframework.jdbc.datasource.DriverManagerDataSource">
      <property name ="driverClassName" value ="com.mysql.jdbc.Driver"/>
      <property name ="url" value ="jdbc:mysql://localhost:3306/branch_db"/>
      <property name ="username" value ="root"/>
      <property name ="password" value ="root"/>
    </bean>

    <bean id ="jdbcTemplate" class               
      ="org.springframework.jdbc.core.JdbcTemplate">
      <property name ="dataSource" ref ="dataSource"/>
    </bean>

    <bean id ="employeeDao" class  ="com.springdao.EmployeeDaoImpl">
      <property name ="jdbcTemplate" ref ="jdbcTemplate"/>
    </bean>

</beans>
sda
  • 27
  • 1
  • 5
  • Thank you, please see above edit for exception. – sda Dec 07 '16 at 18:05
  • It says exception in `EmployeeDaoImpl.saveEmployee`. Could you please check your saveEmployee method. – SachinSarawgi Dec 07 '16 at 18:06
  • There is no exception in parsing otherwise, it could have given ParseException. – SachinSarawgi Dec 07 '16 at 18:08
  • `private JdbcTemplate jdbcTemplate` of the `EmployeeDaoImpl` class is `null`. Is it a best-practice to initialize it in the `main` method as `empDao.setJdbcTemplate((JdbcTemplate) ctx.getBean("jdbcTemplate"));`? – sda Dec 07 '16 at 20:18

0 Answers0