我尝试调出类别列表以在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```