0

I've problem wth any operation on ijected repository.

Exception in thread "JavaFX Application Thread" java.lang.RuntimeException:  java.lang.reflect.InvocationTargetException
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
    at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Node.fireEvent(Node.java:8413)
    at javafx.scene.control.Button.fire(Button.java:185)
    at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
    at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
    at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
    at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:381)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:417)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:416)
    at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
    at com.sun.glass.ui.View.notifyMouse(View.java:937)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    at java.lang.Thread.run(Thread.java:748)
 Caused by:
 java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
    at sun.reflect.GeneratedMethodAccessor21.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
    ... 48 more
 Caused by: java.lang.NullPointerException
    at pl.com.jg.serviceapp.controllers.ReportFrameController.generateButtonClicked(ReportFrameController.java:54)
    ... 58 more

My ReportFrameController (using trouble repo):

@Controller
public class ReportFrameController{

    @FXML
    private Button generateButton;

    @FXML
    private Button cancelButton;

    @FXML
    private TextArea textReport;

    @Autowired
    @Qualifier(value = "kr")
    private KeywordRepository keywordRepository;

    @FXML
    public void cancelButtonClicked(ActionEvent event) throws IOException {

        Parent root = FXMLLoader.load(getClass().getResource("/fxml/menuFrame.fxml"));
        Scene scene = new Scene(root);
        Stage stage = (Stage) cancelButton.getScene().getWindow();
        stage.hide();
        stage.setTitle("Logged");
        stage.setScene(scene);
        stage.show();
    }

    @FXML
    public void generateButtonClicked(ActionEvent event) throws NullPointerException{

        String word, checking;
        checking = textReport.getText().toLowerCase();


        for (Keyword i : keywordRepository.findAll()) {  //InteliJ suggest trouble on this line

            word = i.getName();
            System.out.println("Word: " + word);
            System.out.println("Checking: " + checking);

            if (checking.contains(word)) {
                System.out.println("I found the key! " + word);
            } else System.out.println("I didn't found the key! " + word);

        }

        Stage stage = (Stage) generateButton.getScene().getWindow();
        stage.close();
    }
}

Operation on another repo are ok:

@Controller
public class MainFrameController {

    @FXML
    public Button connectButton;

    @FXML
    private PasswordField passwordBox;

    @FXML
    private TextField usernameBox;

    @FXML
    private Label answerLb;

    @Autowired
    UserController userController;

    @FXML
    void connectClicked(ActionEvent event) throws Exception {
        String login;
        String password;
        String answer;

        login = usernameBox.getText();
        password = MD5(passwordBox.getText());

        answer = userController.auth(login, password);
        answerLb.setText(answer);

        if (answer.equals("ok")) {
            try {
                Parent root = FXMLLoader.load(getClass().getResource("/fxml/menuFrame.fxml"));
                Scene scene = new Scene(root);
                Stage stage = (Stage) connectButton.getScene().getWindow();
                stage.hide();
                stage.setTitle("Logged in as: " + login);
                stage.setScene(scene);
                stage.show();

            } catch (IOException e) {
                System.out.println("Failed to create new Window.");
            }

        }
    }


    public String MD5(String s) throws Exception{

        MessageDigest m=MessageDigest.getInstance("MD5");
        m.update(s.getBytes(),0,s.length());
        String pass = new BigInteger(1,m.digest()).toString(16);
        return(pass);
    }

Both of repo have suitable models. Entities in DB have a few entries. Need sth else?

Edit. Add main class:

@SpringBootApplication
@EnableAutoConfiguration
@ComponentScan(basePackages = {"pl.com.jg.serviceapp.controllers",
    "pl.com.jg.serviceapp.services"})
@EntityScan("pl.com.jg.serviceapp.models")
@EnableJpaRepositories(basePackages = {"pl.com.jg.serviceapp.repositories"})
public class ServiceappApplication extends Application{

private ConfigurableApplicationContext springContext;
private Parent rootNode;

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

@Override
public void init() throws Exception {
    springContext = SpringApplication.run(ServiceappApplication.class);
    FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/fxml/mainFrame.fxml"));
    fxmlLoader.setControllerFactory(springContext::getBean);
    rootNode = fxmlLoader.load();
}

@Override
public void start(Stage stage) throws Exception {
    stage.setScene(new Scene(rootNode));
    stage.setTitle("SERVICEAPP");
    stage.show();
}

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


}

Edit:

Add MenuFrameController:

@Controller
public class MenuFrameController {

@FXML
private Button generateReportButton;



@FXML
void generateReportButtonClicked(ActionEvent event) throws Exception {

        try {
            Parent root = FXMLLoader.load(getClass().getResource("/fxml/reportFrame.fxml"));
            Scene scene = new Scene(root);
            Stage stage = (Stage) generateReportButton.getScene().getWindow();
            stage.setTitle("Generate new report");
            stage.setScene(scene);
            stage.show();
        } catch (IOException e) {
            System.out.println("Failed to create Generate Window.");
        }



    }

}
9
  • 1
    NullPointerException is quite clear. It is thown at ReportFrameController.java:54 , which line would that be exactly? Commented May 21, 2018 at 19:19
  • How are you ensuring the controller is a spring-managed bean? Commented May 21, 2018 at 19:25
  • line 54: for (Keyword i : keywordRepository.findAll()) { //InteliJ suggest trouble on this line Commented May 21, 2018 at 19:33
  • Edit. Added mainClass Commented May 21, 2018 at 19:39
  • But the main class is only loading mainFrame.fxml (and I assume MainFrameController is the controller class for that FXML file). Where are you loading the FXML that uses ReportFrameController? Commented May 21, 2018 at 19:50

1 Answer 1

2

You are not setting the controller factory on the FXMLLoader when you load reportFrame.fxml. Consequently, when the FXMLLoader needs an instance of ReportFrameController, it just instantiates one directly, instead of getting one from the Spring application context. Since spring is not managing the controller, nothing can be injected into it.

Just use the same pattern to load reportFrame.fxml that you used to load mainFrame.fxml. You can inject the ApplicationContext itself into the controller in order to do this. Note that this means your MenuFrameController must also be spring managed (so use the same technique when you load its corresponding FXML file, etc).

Also, as an aside, you should make all your controller prototypes, since each time you load the same FXML file, you will need a new instance of the controller.

@Controller
@Scope("prototype")
public class MenuFrameController {

@FXML
private Button generateReportButton;

@Autowired
private ApplicationContext springContext ;

@FXML
void generateReportButtonClicked(ActionEvent event) throws Exception {

        try {
            FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/fxml/reportFrame.fxml"));
            fxmlLoader.setControllerFactory(springContext::getBean);
            Parent root = fxmlLoader.load();
            Scene scene = new Scene(root);
            Stage stage = (Stage) generateReportButton.getScene().getWindow();
            stage.setTitle("Generate new report");
            stage.setScene(scene);
            stage.show();
        } catch (IOException e) {
            System.out.println("Failed to create Generate Window.");
        }



    }

}

If you like you can reduce the repetitive code by creating a class that encapsulates the loading functionality, and inject an instance of it wherever you need it:

public class SpringFXMLLoader {

    @Autowired
    private ApplicationContext springContext ;

    public Parent load(String resourceName) throws IOException {
        FXMLLoader loader = new FXMLLoader(getClass().getResource(resourceName));
        loader.setControllerFactory(springContext::getBean);
        return loader.load();
    }
}

Then your main class can do

@Override
public void init() throws Exception {
    springContext = SpringApplication.run(ServiceappApplication.class);
    rootNode = springContext.getBean(SpringFXMLLoader.class).load("/fxml/mainFrame.fxml");
}

and your MenuFrameController simplifies (a little) to

@Controller
@Scope("prototype")
public class MenuFrameController {

@FXML
private Button generateReportButton;

@Autowired
private SpringFXMLLoader fxmlLoader ;

@FXML
void generateReportButtonClicked(ActionEvent event) throws Exception {

        try {
            Parent root = fxmlLoader.load("/fxml/reportFrame.fxml");
            Scene scene = new Scene(root);
            Stage stage = (Stage) generateReportButton.getScene().getWindow();
            stage.setTitle("Generate new report");
            stage.setScene(scene);
            stage.show();
        } catch (IOException e) {
            System.out.println("Failed to create Generate Window.");
        }



    }

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.