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!