1

There are a lot of topics with a similar problem, but i don't get what i'm doing wrong.

I want to build a small Application with JavaFx. Therefore i have a controller-class, in which i want to inject a service. The service is responsible for parsing a XML file and giving back a useable data structure.

So this is my Code right now:

AppConfig class:

@Configuration
@ComponentScan("de.freddy.services")
@PropertySource("classpath:de/freddy/application.properties")
public class AppConfig {
    public AdressbuchService adressbuchService() {
        AdressbuchService aService = new AdressbuchService();
        return aService;
    }
}

Main-Application:

public class MeyerNewsletter extends Application {
    private Stage primaryStage;
    private Parent rootLayout;
    private ApplicationContext appContext;

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage){
        appContext = new   AnnotationConfigApplicationContext(AppConfig.class);
        this.primaryStage = primaryStage;
        this.primaryStage.setTitle("Newsletterverwaltung");           
        System.out.println(appContext.getEnvironment().getProperty("application.name") + " wurde gestartet.");
        initRootLayout();
    }

    private void initRootLayout() {
        try {
            rootLayout = FXMLLoader.load(getClass().getResource("/de.freddy/fxml/RootView.fxml"));
            primaryStage.setTitle("Newsletterverwaltung");
            primaryStage.setScene(new Scene(rootLayout));
            primaryStage.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

And at last the RootController, where the service will be used:

public class RootController {

    @Autowired
    private AdressbuchService adressbuchService;

    @FXML
    public void ladeAdressbuch() {
        AdressListe liste = adressbuchService.liesAlleAdressen();
        for (Adresse a : liste.getAdressListe()) {
            System.out.println(a);
        }
    }

    @FXML
    public void speichereAdressbuch() {
        System.out.println("test2 - speichere");
    }
}    

The moment i call the function "ladeAdressbuch()", a null pointer Exception comes up at the line, where i use the adressbuchService Object in Rootcontroller.

So, what am i doing wrong?

Thanks for help!

Freddy
  • 156
  • 1
  • 8
  • Does your Spring controller knows about your `RootController`? Any configuration for that? – Abdullah Khan Feb 04 '18 at 11:58
  • How do i know? I'm totally new to Spring and Injection. – Freddy Feb 04 '18 at 12:03
  • I tried and it didn't work.. What do you mean with "you might have to add configure Spring with your Javafx properly"? – Freddy Feb 04 '18 at 13:12
  • Try annotating your controller with `@Component`. In the last comment i wrote `@ComponentScan` instead of @Component. Sorry for that. – Abdullah Khan Feb 04 '18 at 13:15
  • 1
    I tried @Component also, because i thought you wanted to say that.. – Freddy Feb 04 '18 at 13:21
  • The only thing i can imagine is that the RootController is never manually instantiated. Only „injected“ via FXML – Freddy Feb 04 '18 at 13:25
  • Your controller is created by the `FXMLLoader`; it is not managed by Spring and so the auto wired field cannot be injected. – James_D Feb 04 '18 at 14:16

0 Answers0