JavaFX:线程“ JavaFX应用程序线程”中的异常java.lang.RuntimeException:java.lang.reflect.InvocationTargetException

我尝试调出类别列表以在tableView中显示它,但是当我在控制器中注入CategoryService时出现了异常,这是我的代码的一部分, 当我单击onbtnactionCategory时,家庭控制器将执行一个操作以使用stackPane提取场景的一部分内容,该场景的一部分应使用新视图进行更改

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

    }
} 

我的类别 但是当初始化方法称为witch时,我调用我的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);

    }
}

我的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> 

这是我的例外,一些帮助。

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```
kjhsdgfhkjshfuwerytu 回答:JavaFX:线程“ JavaFX应用程序线程”中的异常java.lang.RuntimeException:java.lang.reflect.InvocationTargetException

未使用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初始化)。以下线程提供了几种初始化此类字段的选项:Passing Parameters JavaFX FXML

如果您使用与上述类似的代码加载所有fxml,则应该能够通过spring简单地注入该值。

本文链接:https://www.f2er.com/2774570.html

大家都在问