JavaFX:线程异常 "JavaFX Application Thread" java.lang.RuntimeException:java.lang.reflect.InvocationTargetException
JavaFX : Exception in thread "JavaFX Application Thread" 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) {
}
}
我的分类控制器
但是当调用初始化方法时 我调用我的 CategoryService 来恢复对象为空的数据
@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$null7(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
进行初始化)。以下线程中介绍了初始化此类字段的几个选项:Passing Parameters JavaFX FXML
如果您使用与上述类似的代码加载所有 fxml,您应该能够通过 spring.
简单地注入该值
我尝试恢复类别列表以在 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) {
}
}
我的分类控制器 但是当调用初始化方法时 我调用我的 CategoryService 来恢复对象为空的数据
@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$null7(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
进行初始化)。以下线程中介绍了初始化此类字段的几个选项:Passing Parameters JavaFX FXML
如果您使用与上述类似的代码加载所有 fxml,您应该能够通过 spring.
简单地注入该值