Shashank Anand
Shashank Anand

Reputation: 35

Getting nullpointerException while using the jdbcTemplate in spring

I am new to Spring, and working on project where I need to get some data from a Database. I am using tomcat server and using JNDI DB connection pooling. Below is my code and Spring configuration. I am getting aNullPointerException because jdbcTemplate is null.

public class AppConfig 
{
@Autowired
private JdbcTemplate jdbcTemplate;
private static AppConfig config=null;
private HashMap<String,  String> dbAppParameterValuesCacheMap;
public AppConfig()
{
    cacheConfig();

}

public boolean cacheConfig()
{
    dbAppParameterValuesCacheMap = null;
    List<Map<String, Object>> appConfigMapList=null;
    String parameterType="PP_APP_CONFIG";
    try
    {
        appConfigMapList= jdbcTemplate
            .queryForList("SELECT  Parameter_Value, Parameter_Name FROM PP_Application_Parameter where PARAMETER_TYPE='"+parameterType+"'");
    }
    catch(NullPointerException ex)
    {
        System.out.println("here");
        ex.printStackTrace();
    }
    if (dbAppParameterValuesCacheMap == null)
        dbAppParameterValuesCacheMap = new HashMap<String,String>();
    for(Map<String, Object> configMap:appConfigMapList)
    {
            dbAppParameterValuesCacheMap.put((String)configMap.get("Parameter_Name"), (String)configMap.get("Parameter_Value"));
    }

    return true;
}
}

My Spring configuration file contains:

<bean id="dbDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="java:comp/env/jdbc/PP_DATASOURCE" />
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" lazy-init="false">
    <property name="dataSource" ref="dbDataSource"></property>
</bean>
<bean id="config" class="AppConfig" scope="singleton">
</bean>

JNDI has been created successfully. I am getting a NullPointerException when it tries to execute the line

appConfigMapList= jdbcTemplate
            .queryForList("SELECT  Parameter_Value, Parameter_Name FROM PP_Application_Parameter where PARAMETER_TYPE='"+parameterType+"'");

Upvotes: 3

Views: 1517

Answers (2)

Jan
Jan

Reputation: 13858

Consider this change:

public AppConfig()
{
}

@PostConstruct
public boolean cacheConfig()
{

This will move access to jdbcTemplate to a time after the autowirings has been done by Spring while keeping your semantics (run cacheConfig right after object construction) in place.

@Autowired fields are null while the constructor runs.

Upvotes: 0

Chris Nauroth
Chris Nauroth

Reputation: 9854

As per the documentation of Autowired, injection happens after construction of the bean.

Fields are injected right after construction of a bean, before any config methods are invoked. Such a config field does not have to be public.

Since your code is attempting to reference jdbcTemplate from the constructor, it has not yet been injected, and therefore it is null.

If your goal is to run some extra initialization code after the auto-wired dependencies are guaranteed to be in place, then one way to do that is to annotate a different method from the constructor with PostContruct.

Upvotes: 1

Related Questions