1

I am trying to autowire my datamodel class into various controller classes using the Autowired annotation and I keep getting a null pointer exception, I would like to keep my configuration XML based and avoid using the application context implementation if possible. a snippet of the code and spring-config file is below

Spring-Config

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-2.5.xsd">

    <context:annotation-config/>

    <bean id="datamodel" class="com.filemanager.datamodel.DataModel" autowire="byType"/>

</beans>

Data Model Class

public class DataModel {

    private ObservableList<Song> windowDisplaySongs;

    public void test() {
        System.out.println("Hey, this is working");
    }

    public ObservableList<Song> getWindowDisplaySongs() {
        return windowDisplaySongs;
    }
    public void setWindowDisplaySongs(ObservableList<Song> windowDisplaySongs) {
        this.windowDisplaySongs = windowDisplaySongs;
    }
}

Controller that has dependency on Model

public class FileBrowserController implements Initializable {

    @Autowired
    DataModel dataModel;

    @Override
    public void initialize(URL location, ResourceBundle resources) {

        dataModel.test();
    }
}

Things that I have tried

  • Using the component annotation above the model class, and above the model and controller class

  • Using the controller annotation above the controller class

  • Renaming the datamodel reference to match the name within the spring-config file

  • Using the qualifier annotation to specify that the datamodel reference should match the name in the spring config file.

  • Using the model reference in a method other than the init method

  • Removing the autowire="byType" from the config file

  • Creating an application context in the main method by using the code below, I was able to use the getBean() method with this, but I would like to use autowired to automatically inject the dependencies instead of having to call the getBean() method each time.

    ApplicationContext ctx = new ClassPathXmlApplicationContext("config/spring-config.xml");
    
  • Following the solutions to other null pointer spring issues already on Stackoverflow:

oznomal
  • 439
  • 2
  • 8
  • 22

1 Answers1

1

The FileBrowserController class should be annotated as @Controller so spring knows it should inject properties there. Also @Autowiring without qualifier checks if you have a bean with the name of the property. In your case:

@Autowired
DataModel dataModel;

Check for bean with id dataModel but your bean is defined with different name :

<bean id="datamodel"...

Spot the difference? :) you can either change the bean id or you can use Qualifier to make it even clearer

@Autowired
@Qualifier("datamodel")
DataModel dataModel;
Veselin Davidov
  • 7,031
  • 1
  • 15
  • 23
  • Thank you for your I help, however, I tried the solution and still didn't have any luck but just for a little clarification, isn't the controller annotation the same as the component annotation? I was always under the impression that component was basically the generic stereotype and the controller, service, and repository stereotypes were more specialized but they all accomplished the same thing? – oznomal Apr 11 '18 at 09:19