Search code examples
javaspringnullpointerexceptionjdbctemplate

spring jdbctemplate: null pointer exception in set values


my error

 java.lang.NullPointerException.
 MyDAO$2.setValues
    org.springframework.jdbc.core.JdbcTemplate$2.doInPreparedStatement(JdbcTemplate.java:680)
    org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:454)
    org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:676)
    org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:738)

net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:149)
org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:694)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:122)
org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:144)
org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:639)

my code is

     this.jdbcTemplate.update(SOME_SQL_UPDATE, new PreparedStatementSetter() {
                    public void setValues(PreparedStatement ps) throws SQLException {
                        ps.setBoolean(1, myObj.isVal1());
                        ps.setString(2, myObj.getVal2().toString());
                    }
                });

public class myObj {
    private boolean val1;
    private Enum val2;
}

my db column for boolean is number(1,0) and my set string column is varchar.

Note: I am using spring 1.2 and currently upgrading to spring 3.

What could be wrong?

How can i fix this?


Solution

  • Are you sure myObj is not null? It looks like it could be the only object that could be null in that statement. Could you do a simple null check on the object inside the method to confirm?