簡體   English   中英

JavaFX:線程“JavaFX 應用程序線程”中的異常 java.lang.RuntimeException: java.lang.reflect.InvocationTargetException

[英]JavaFX : Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException

我嘗試恢復類別列表以在 tableView 中顯示它,但是當我在我的控制器中注入 CategoryService 時出現異常,這是我的代碼的一部分,當我使用 stackPane 時,主控制器包含一個操作來掃描我的場景的一部分單擊 onbtnActionCategory 場景的一部分應該隨着新視圖而改變

@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) {

    }
} 

我的 categoryCotroller 但是當初始化方法調用女巫時,我調用我的 CategoryService 來恢復對象為 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);

    }
}

我的類別.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> 

這是我的例外,一些幫助。

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```

控制器實例不是使用 spring 的BeanFactory創建的。 因此,不會注入@Autowired值。 您需要告訴FXMLLoader使用BeanFactory創建控制器:

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);
}

當然,要使其工作,您需要確保BeanFactory context字段可用(通常用ApplicationContext初始化)。 以下線程提供了初始化此類字段的幾個選項: 傳遞參數 JavaFX FXML

如果您使用與上述類似的代碼加載所有 fxml,您應該能夠通過 spring 簡單地注入該值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM