0

I keep geting NPE with Spring injecting Datasource in following code.

I got two classes. Superclasss

public class RepositorySource extends PropertiesConfiguration{

    private RepositoryView repositoryView;

    public RepositoryView getRepositoryView() {
        return repositoryView;
    }
    public void setRepositoryView(RepositoryView repositoryView) {
        this.repositoryView = repositoryView;
    }               
}

And subclass

public class RepositoryView {

    private DataSource dataSource;

    public DataSource getDataSource() {
        return dataSource;
    }

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    public void testConn_RV(
        Connection con;
        try {
            con = dataSource.getConnection();       
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

Here is the bean definition to inject the DataSource from it:

<bean id="RepositorySourceBean" class="com.acme.persistence.metamodel.views.impl.RepositorySource" >
<property name="repositoryView">
                <bean class="com.acme.persistence.metamodel.views.impl.RepositoryView">         
                        <property name="dataSource" ref="mysqlDataSource">  </property>
                </bean>     
    </property>
</bean>

And the Datasource bean:

<!-- DATABASE PROPERTIES LOCALIZATION -->
        <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

            <property name="location">
                <value>properties/mysql-persistence.properties</value>
            </property>             
        </bean>

    <!-- DATASOURCE DEFINITION -->    
        <bean id="mysqlDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">

            <property name="driverClassName" value="${jdbc.driverClassName}" />
            <property name="url" value="${jdbc.url}" />
            <property name="username" value="${jdbc.username}" />
            <property name="password" value="${jdbc.password}" />
        </bean>

I keep getting the NPE in last line of main method:

public static void main( String[] args )
    {           
        App app = new App();            
        ApplicationContext appContext = 
                new ClassPathXmlApplicationContext("spring/configBeans/BeanLocations.xml");

    RepositorySource src = new RepositorySource();  
    src.getRepositoryView().testConn_RV();} //<----- NPE HERE

It seems DataSource is not beeing initialized but why?

How to solve this?

EDIT This part:

 RepositorySource src = new RepositorySource(); 
        src.getRepositoryView().testConn_RV();} 

needs to go in method of main class called say initResourceSource() so i would have to pass appContext to the method so therefore getBean() is no the solution

2

2 Answers 2

1

It should be like this...

 ApplicationContext appContext = new ClassPathXmlApplicationContext("spring/configBeans/BeanLocations.xml");
 RepositorySource src = appContext.getBean("repositorySourceBean",RepositorySource.class);  
9
  • getBean() is NOT DI. You should avoid it. I am after DI with Spring. Read the link from my above post plz.
    – mCs
    Aug 6, 2013 at 13:45
  • Then, instead of getBean() you can use XML configuration for injecting bean. But in that case also, you have to declare your class variable(RepositorySource). Aug 6, 2013 at 13:47
  • That is the code I have psoted. It is not working due to NPE.
    – mCs
    Aug 6, 2013 at 13:49
  • I referred to the link, you mentioned above. Read carefully, he is saying that getBean() is not IOC, but then he is telling to use XML based dependency injection. Aug 6, 2013 at 13:52
  • I understand but that is exactly what is in my example that is not working.The `ResourceSource1 is not app main class. The DI is not working here, why?
    – mCs
    Aug 6, 2013 at 14:13
0

Answer in continuation to comment... Use this:

private RepositorySource src;

public void setSrc(RepositorySource src){
    this.src = src;
}


public static void main( String[] args )
{           
    App app = new App();            
    ApplicationContext appContext = new ClassPathXmlApplicationContext("spring/configBeans/BeanLocations.xml");

    //RepositorySource src = new RepositorySource();  
    src.getRepositoryView().testConn_RV();
}

and inject this using XML

2
  • This seems bit ugly solution. The RepositorySource is not supposed to be a global variable for the main app. This is workaround but it is not solving the problem. I dont believe this can not be solved by constructor-arg for ResourceSource like they suggest HERE
    – mCs
    Aug 6, 2013 at 15:25
  • Actually the RepositorySource is going to be used in a method of a main app.
    – mCs
    Aug 6, 2013 at 15:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.