Chun ping Wang
Chun ping Wang

Reputation: 3929

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?

Upvotes: 0

Views: 2406

Answers (3)

Ousama
Ousama

Reputation: 2810

I fixed this issue by using setObject instead of setBoolean and setString :

this.jdbcTemplate.update(SOME_SQL_UPDATE, new PreparedStatementSetter() {
  public void setValues(PreparedStatement ps) throws SQLException {
    ps.setObject(1, myObj.isVal1() ? 1 : 0, Types.NUMERIC);
    ps.setObject(2, myObj.getVal2().toString(), Types.VARCHAR);
  }
});

NB: In my case, I'm working with Oracle 12c, boolean is stocked as a number (true => 1, false => 0)

Upvotes: 0

Jeshurun
Jeshurun

Reputation: 23186

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?

Upvotes: 1

hvgotcodes
hvgotcodes

Reputation: 120308

in your class myObj, the values for val1 and val2 are never initialized.

Upvotes: 0

Related Questions