2

As the title says, the repositories of our application don't get initialised, but we're not instantiating either, as other fixes online have suggested.

This is the game launcher of the app.

    public class JavaFxApplication extends Application {

    private static ConfigurableApplicationContext context;

    public static void main(String[] args) {
        Application.launch(JavaFxApplication.class, args);
    }

    @Override
    public void init() throws Exception {
        ApplicationContextInitializer<GenericApplicationContext> initializer =
            ac -> {
                ac.registerBean(Application.class, () -> JavaFxApplication.this);
                ac.registerBean(Parameters.class, this::getParameters);
                ac.registerBean(HostServices.class, this::getHostServices);
            };
        context = new SpringApplicationBuilder()
                .sources(PacManLauncher.class)
                .initializers(initializer)
                .run(getParameters().getRaw().toArray(new String[0]));
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        primaryStage.setTitle("Pac-Man");
        primaryStage.getIcons().add(new Image("images/pacman/pacman.png"));
        Parent root = new LoginPage(primaryStage);
        Scene scene = new Scene(root, 600, 400);
        scene.getStylesheets().add("stylesheet/stylesheet.css");
        primaryStage.setScene(scene);

        primaryStage.show();
    }

    @Override
    public void stop() throws Exception {
        context.close();
        Platform.exit();
    }
}

This is the login authentication page.

//more code
  @Autowired
    private UserRepository userRepository;

    private static BCryptPasswordEncoder passwordEncoder;

    /**
     * Instantiates a new Authentication service with the User repository.
     *
     * @param userRepository the user repository
     */

    public AuthenticationService(UserRepository userRepository) {

        this.userRepository = userRepository;
    }

   /**
     * Login form.
     *
     * @param username the username
     * @param password the password
     * @return a boolean (true if user is correctly logged in, false if the opposite)
     */
    public boolean login(String username, String password) {

        passwordEncoder = new BCryptPasswordEncoder();
        Optional<User> user = userRepository.findByName(username);
        if (user.isPresent()) {
            if (passwordEncoder.matches(password, 
user.get().getPassword())) {
                UserSession.getInstance(username, user.get().getId());
            }
        }
        return (UserSession.getUserId() != null && UserSession.getUserName() != null);
    }
//more code

The login code works in a seperate project, but in this as a whole, gives us a nullpointer on the autowiring. Does anyone know how we can get a repository to be made without a nullpointer coming up or can you at least guide us to the right direction with some sources.

Thanks in advance.

EDIT: I put up the wrong game launcher on the app, so if anyone can help with this one, it would be appreciated. :)

jewelsea
  • 150,031
  • 14
  • 366
  • 406
Mu Elay
  • 23
  • 5
  • As an FYI, (it's not the issue causing the auto wiring issue you are asking about), you don't need to call `Platform.exit()` in your application `stop` method. By definition, if the application is stopping, the platform is already in the process of exiting. – jewelsea Dec 05 '19 at 18:37
  • There is some info on working with Spring and JavaFX in the answer to: [Adding Spring Dependency Injection in JavaFX (JPA Repo, Service)](https://stackoverflow.com/questions/57887944/adding-spring-dependency-injection-in-javafx-jpa-repo-service). It may or may not help you. One weird thing in your setup is that you call `SpringApplication.run(JavaFxApplication.class)`, but you annotate class `PacManLauncher` as `@SpringBootApplication`. If you still have issues, you may help to supply a [mcve]. – jewelsea Dec 05 '19 at 18:43
  • I'd advise having the `main` method in your JavaFX application class (so that is the class which is run by the `java` launcher and takes in arguments). Have your SpringApplication as a separate class, and in the `init` method of your JavaFX class, launch your Spring application class (using `SpringApplication.run` or a `SpringApplicationBuilder`). If it still doesn't work, also try [turning on Spring debug level logging](https://www.baeldung.com/spring-boot-logging), to try and understand what Spring did or didn't do. – jewelsea Dec 05 '19 at 18:50
  • Sorry, i put up the wrong launcher when I looked this morning, if it isn't too much trouble, can you look at the launcher i put up now? – Mu Elay Dec 06 '19 at 10:14

1 Answers1

2

Edit

As you have updated your question I have updated my answer as well. And I'm still pointing out the similar thing that I have described before!

  • You are missing @SpringBootApplication annotation on your JavaFxApplication class and seems like you have it in your PacManLauncher
  • Your JavaFxApplication main-class (annotated with @SpringBootApplication) must be in the parent package and repositories , components, configuration classes in the children directory so that spring-boot will inject/configure all required components, configurations and repositories.

    Spring-Boot application directory tree structure

I have updated your code as well,

@SpringBootApplication
public class JavaFxApplication extends Application {

    private static ConfigurableApplicationContext context;

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

    @Override
    public void init() throws Exception {
        context = springBootApplicationContext();
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        primaryStage.setTitle("Pac-Man");
        primaryStage.getIcons().add(new Image("images/pacman/pacman.png"));
        Parent root = new LoginPage(primaryStage);
        Scene scene = new Scene(root, 600, 400);
        scene.getStylesheets().add("stylesheet/stylesheet.css");
        primaryStage.setScene(scene);

        primaryStage.show();
    }

    @Override
    public void stop() throws Exception {
        context.close();
    }

     private ConfigurableApplicationContext springBootApplicationContext() {
        SpringApplicationBuilder builder = new SpringApplicationBuilder(JavaFxApplication.class);
        String[] args = getParameters().getRaw().stream().toArray(String[]::new);
        return builder.run(args);
    }
}

PS: I have removed your code from init() method which may not required as the above code can inject every spring components. I also removed source -> PacManLauncher class as well. Please run JavaFxApplication .. I hope it should fix your issue:)

Shekhar Rai
  • 2,008
  • 2
  • 22
  • 25
  • Thanks fo this! But i am sorry, this might not be the fix, because I put up old launcher code and i edited the new one in, if you don't mind, could you maybe look at it again and see if the fix is comparable or not? Again, sorry if this might be annoying! – Mu Elay Dec 06 '19 at 10:17
  • @MuElay It's ok as you are new to this community - but make sure that you read the documentation before asking any questions, and you're allowed to update(if needed) the question :) I have updated the answer as well - please have a look on it... :) – Shekhar Rai Dec 06 '19 at 11:12
  • 1
    Yes! This seems to work! Thank you so much, didn't even notice those things, haha. Quite new to spring boot. Thanks for the help! :) – Mu Elay Dec 06 '19 at 14:27