JavaFX:thread中的异常;JavaFX应用程序线程;java.lang.RuntimeException:java.lang.reflect.InvocationTargetException
我尝试重新浏览category列表以在tableView中显示它,但当我在控制器中插入CategoryService时遇到了一个异常,这是代码的一部分, 当我单击onbtnActionCategory时,主控制器将包含一个操作,以使用stackPane切换场景的一部分。场景的一部分应随新视图而更改JavaFX:thread中的异常;JavaFX应用程序线程;java.lang.RuntimeException:java.lang.reflect.InvocationTargetException,java,spring-boot,javafx,javafx-2,fxml,Java,Spring Boot,Javafx,Javafx 2,Fxml,我尝试重新浏览category列表以在tableView中显示它,但当我在控制器中插入CategoryService时遇到了一个异常,这是代码的一部分, 当我单击onbtnActionCategory时,主控制器将包含一个操作,以使用stackPane切换场景的一部分。场景的一部分应随新视图而更改 @Controller public class HomeController implements Initializable { @FXML private StackPane s
@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) {
}
}
我的分类电脑
但是,当initialize方法调用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);
}
}
控制器实例不是使用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上下文
字段可用(通常使用ApplicationContext
初始化)。以下线程中提供了几种初始化此类字段的选项:
如果您使用与上面类似的代码加载所有FXML,您应该能够通过spring简单地注入该值
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```
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);
}