-1

I am trying to get data from mysql database.

My method for EmployeeJDBCTemplate is

public List<Employee> getListEmployees() {
    String sql = "select * from testemp";
    List<Employee> listEmp = jdbcTemplaeObject.query(sql, new RowMapper<Employee>() {

        @Override
        public Employee mapRow(ResultSet rs, int rowNum) throws SQLException {
            Employee emp = new Employee();

            emp.setSn(rs.getInt("sn"));
            emp.setID(rs.getInt("ID"));
            emp.setName(rs.getString("name"));
            emp.setCheckin(rs.getString("checkin"));
            emp.setCheckout(rs.getString("checkout"));
            emp.setBreakstart(rs.getString("breakstart"));
            emp.setBreakend(rs.getString("breakend"));

            return emp;
        }

    });

    return listEmp;
}

And this is the error I got after running the program:

Caused by: java.lang.NullPointerException
    at att.user.dao.EmployeeJDBCTemplate.getListEmployees(EmployeeJDBCTemplate.java:92)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at javax.el.BeanELResolver.invoke(BeanELResolver.java:183)
    at javax.el.CompositeELResolver.invoke(CompositeELResolver.java:161)
    at org.apache.el.parser.AstValue.getValue(AstValue.java:173)
    at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:184)
    at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:109)
    ... 77 more

Can anybody help me? Thank you in advance.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
sandesh
  • 59
  • 2
  • 7
  • And DONT do this amount of work in a getter. [Bad practice](http://stackoverflow.com/questions/2090033/why-jsf-calls-getters-multiple-times) – Kukeltje Feb 25 '15 at 22:01

2 Answers2

1

Looks like employeeJDBCTemplate object is not initialize - is null. Have you initialize it.

  • Hi Marcin, Where should I initialize employeeJDBCTemplate exactly? – sandesh Feb 26 '15 at 00:18
  • I found samething similar, look here https://github.com/scooby-doo/SpringExample/blob/master/src/com/endava/spring/jdbcTemplate/EmployeeJDBCTemplate.java. You should initialize it in a constructor or spring configuration file. – Marcin Łojewski Feb 26 '15 at 07:07
1

Please replace the below tag

<p:dataTable var="emp" value="#{employeeJDBCTemplate.getListEmployees()}">

with

<p:dataTable var="emp" value="#{employeeJDBCTemplate.listEmployees()}">

Also please check if you initialize the employeeJDBCTemplate. template Initialization in Spring XML configuration or using annotations

Swathi
  • 602
  • 4
  • 17