0

Am using Java 1.7 and Spring Framework 4.3.4.RELEASE.

Configured a DAO and can't seem to even step inside a finder method inside calling class which is designed to use the DAO.

/src/main/resources/database-config.xml:

<bean id="dataSourceProducts" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="${db.driver}" />
    <property name="url" value="${db.urlProducts}" />
    <property name="username" value="${db.username}" />
    <property name="password" value="${db.password}" />
    <property name="initialSize" value="${db.initialSize}"/>
    <property name="maxTotal" value="${db.maxTotal}"/>
    <property name="maxIdle" value="0"/>
    <property name="minIdle" value="0"/>
    <property name="maxWaitMillis" value="10000"/>
    <property name="validationQuery" value="SELECT NOW();"/>
    <property name="timeBetweenEvictionRunsMillis" value="10000"/>      
    <property name="removeAbandonedOnMaintenance" value="true"/>
    <property name="maxConnLifetimeMillis" value="10000"/>
    <property name="minEvictableIdleTimeMillis" value="1000"/>

/src/main/webapp/WEB-INF/mvc-dispatcher-servlet.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
    xmlns:cache="http://www.springframework.org/schema/cache"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/cache
        http://www.springframework.org/schema/cache/spring-cache.xsd
        http://www.springframework.org/schema/util 
        http://www.springframework.org/schema/util/spring-util.xsd">

    <import resource="classpath:database-config.xml" />

    <context:component-scan base-package="com.myapp.dao,com.myapp.manager" />

    <mvc:annotation-driven />

/src/main/java/com/myapp/dao/ProductDao.java:

package com.myapp.dao;

@Repository
public class ProductDao {

    public JdbcTemplate jdbcTemplateProduct = null;

    @Autowired  
    @Qualifier("dataSourceProducts")
    DataSource dataSourceProducts;

    ProductDao() { }

    /**
     * constructor to create object and to use for test cases class. 
     * @param jdbcTemplateObject
     */
    @VisibleForTesting
    public ProductDao(JdbcTemplate jdbcTemplateObject) {
        this.jdbcTemplateProduct = jdbcTemplateObject;
    }

    @PostConstruct
    public void init() {
        jdbcTemplateProduct = new JdbcTemplate(dataSourceProducts); 
    }   

    public boolean runValidationQuery() {
        boolean retValue = false;
        SqlRowSet rst = null;
        rst = jdbcTemplateProduct.queryForRowSet("select now()");
        if (rst != null) {
            retValue = true;
        }
        return retValue;
    }
}

/src/main/java/com/myapp/manager/ProductManager:

package com.myapp.manager;

@Controller
public class ProductManager {

    @Autowired
    ProductDao productDao;

    boolean checkValue = false;

    public void check() throws IOException {
        checkValue = productDao.runValidationQuery();
    }

    public static void main(String args []) {
        ProductManager productManager = new ProductManager();
        productManager.check();
    }
 }

This keeps throwing an NullPointerException when the main() method steps onto

productManager.check();

Can't even step into productManager.check();

The same NPE comes right away:


Exception in thread "main" java.lang.NullPointerException
    at com.myapp.manager.ProductManager.check(ProductManager.java:12)
    at com.myapp.manager.ProductManager.main(ProductManager.java:17)

Followed this pattern from other DAOs that are in this project but don't understand what the issue is at hand?

Am suspecting its a configuration issue or a missing annotation issue?

PacificNW_Lover
  • 4,746
  • 31
  • 90
  • 144

0 Answers0