Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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+"'");
like image 366
Shashank Anand Avatar asked Feb 04 '26 18:02

Shashank Anand


1 Answers

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.

like image 175
Chris Nauroth Avatar answered Feb 06 '26 07:02

Chris Nauroth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!