-1

I try to recupere list of category to display it in tableView , but i got an exception when I inject the CategoryService in my controller the are some part my code , the home controller content an action to swetch a part of my scene using stackPane when I click of onbtnActionCategory the part of scene should changed with the new view

@Controller
public class HomeController implements Initializable {
    @FXML
    private StackPane stackPaneHolder;

    public void setPane(Node node) {
        if (stackPaneHolder.getChildren().isEmpty()) {
            //if stackPaneHolder is empty
            stackPaneHolder.getChildren().add(node);

        } else {
            if (stackPaneHolder.getClip() != node) {
                //if stackPaneHolder is not empty then remove existing layer and add new layer
                stackPaneHolder.getChildren().remove(0);
                stackPaneHolder.getChildren().add(0, node);
            }
        }
    }


    public void onBtnActionDashboard(ActionEvent event) throws IOException {
        Node change = FXMLLoader.load(getClass().getResource("/fxml/home.fxml"));
        setPane(change);
    }
    public void onBtnActionCategory(ActionEvent event) throws IOException {
        Node change = FXMLLoader.load(getClass().getResource("/fxml/category.fxml"));
        setPane(change);
    }

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

    }
} 

My categoryCotroller but when the initialize methode called witch I call my CategoryService to recupere the data the object was Null


@Controller
public class CategoryController implements Initializable {

    @Autowired
     CategoryService categoryService;

    @FXML
    private TableView<Category> categoryTable;
    @FXML
    private TableColumn<Category, String> colCategorie;

    @FXML
    private TableColumn<Category, String> colDescription;

    private ObservableList<Category> categoryList = FXCollections.observableArrayList();


    @Override
    public void initialize(URL location, ResourceBundle resources) {
        categoryTable.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
        setColumnProperties();
        loadCategoryDetails();
    }

    private void setColumnProperties() {
        colCategorie.setCellValueFactory(new PropertyValueFactory<>("Category"));
        colDescription.setCellValueFactory(new PropertyValueFactory<>("Description"));

    }

    /*
     *  Add All category to observable list and update table
     */
    private void loadCategoryDetails(){
        categoryList.clear();

        categoryList.addAll(categoryService.findAll());

        categoryTable.setItems(categoryList);

    }
}

My category.fxml



<AnchorPane prefHeight="470.0" prefWidth="840.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"
           fx:controller="com.mojacko.gestionStock.controller.CategoryController" >
    <children>
        <TableView fx:id="categoryTable" layoutX="8.0" layoutY="7.0" prefHeight="451.0" prefWidth="817.0">
            <columns>
                <TableColumn fx:id="colCategorie" prefWidth="244.0" text="Categorie">
                    <!--<cellValueFactory>-->
                        <!--<PropertyValueFactory property="name"/>-->
                    <!--</cellValueFactory>-->
                </TableColumn>
                <TableColumn fx:id="colDescription" prefWidth="595.0" text="Description">
                    <!--<cellValueFactory>-->
                        <!--<PropertyValueFactory property="description"/>-->
                    <!--</cellValueFactory>-->
                </TableColumn>
            </columns>
            <items>

            </items>
            <sortOrder>
                <fx:reference source="colCategorie"/>
            </sortOrder>
        </TableView>
    </children>
</AnchorPane> 

this is my exception , some Help .

at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:431)
    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$147(WinApplication.java:177)
    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.GeneratedMethodAccessor28.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: javafx.fxml.LoadException: 
/C:/Desktop/JFXSB/gestionStock/target/classes/fxml/category.fxml

    at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
    at com.gestionStock.controller.HomeController.onBtnActionCategory(HomeController.java:40)
    ... 58 more
Caused by: java.lang.NullPointerException
    at com.gestionStock.controller.CategoryController.loadCategoryDetails(CategoryController.java:57)
    at com.gestionStock.controller.CategoryController.initialize(CategoryController.java:42)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
    ... 66 more```

1 Answer 1

1

The controller instance is not created using spring's BeanFactory. For this reason the @Autowired value is not injected. You need to tell FXMLLoader to use the BeanFactory to create the controller:

public void onBtnActionCategory(ActionEvent event) throws IOException {
    FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/category.fxml"));
    loader.setControllerFactory(context::getBean);
    Node change = loader.load();
    setPane(change);
}

Of course for this to work you need to make sure a BeanFactory context field is available (usually initialized with ApplicationContext). Several options of initializing this kind of field are presented in the following thread: Passing Parameters JavaFX FXML

If you load all of your fxmls using code similar to the above, you should be able to simply inject that value via spring.

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.