为什么我的弹出窗口与我的ListView发生冲突



嘿,这是我为学校编写的程序的一部分,由于某些原因,我的弹出窗口"event addCustomer"与行冲突:customersList.setItems (oList);

方法setListView()在底部?什么好主意吗?如果你需要程序的其余部分,我可以按要求张贴!

感谢你的帮助!

public class FXMLDocumentController implements Initializable {
    private BankLogic b;
    private ObservableList<String> oList;
    @FXML
    private ListView customersList;
    @FXML
    private Button addCustomerButton, deleteCustomerButton;
    @FXML
    private Button confirmPop1, cancelPop1, confirmPop2, cancelPop2;

    @FXML
    private void addCustomer(ActionEvent event) throws IOException {
        Stage stage;
        Parent root;
        stage = new Stage();
        root = FXMLLoader.load(getClass().getResource("FXMLpopUp1.fxml"));
        stage.setScene(new Scene(root));
        stage.initModality(Modality.APPLICATION_MODAL);
        stage.initOwner(addCustomerButton.getScene().getWindow());
        stage.showAndWait();
    } // opens a confirm window with 2 buttons:
    @FXML
    private void confirmPop1(ActionEvent event) {
        Stage stg = (Stage) ((Node) event.getSource()).getScene().getWindow();
        stg.close();
    }
    @FXML
    private void cancelPop1(ActionEvent event) {
        Stage stg = (Stage) ((Node) event.getSource()).getScene().getWindow();
        stg.close();
    }
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        b = BankLogic.getInstance();

        long nr = 198905643943L;
        b.addCustomer("Kalle karlsson", nr); 
        nr = 198905643843L;
        b.addCustomer("Peter haraldsson", nr);
        nr = 198967643943L;
        b.addCustomer("Hans haraldsson", nr);
        nr = 198905643978L;
        b.addCustomer("Harry haraldsson", nr);
       // 4 objects of a class called Customer!
        setListView(); // calling method to fill ListView 
    }
    public void setListView() {  
        oList = FXCollections.observableArrayList();
        ArrayList<Customer> tC = b.getCustomerList();
        for (Customer c : tC) {
            String s = c.getName() + " " + c.getPnr();
            oList.add(s);
        }
        customersList.setItems(oList); // This line collides with my popup window and givs me an exception.
    }
}
异常:

Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Node.fireEvent(Node.java:8411)
at javafx.scene.control.Button.fire(Button.java:185)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:352)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:275)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:388)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:387)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
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:497)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
... 45 more
Caused by: javafx.fxml.LoadException: file:/NetBeansProjects/Grupp1-Bank/dist/run1287972729/BankSystem.jar!/banksystem/FXMLpopUp1.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 banksystem.FXMLDocumentController.addCustomer(FXMLDocumentController.java:86)
... 55 more
Caused by: java.lang.NullPointerException
at banksystem.FXMLDocumentController.setListView(FXMLDocumentController.java:193)
at banksystem.FXMLDocumentController.initialize(FXMLDocumentController.java:180)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
... 63 more

您正在加载两个不同的FXML文件,因此为您创建了两个不同的控制器。从堆栈跟踪中可以看出,您已经配置了一些东西,以便两个控制器来自同一个类。

FXMLLoader加载FXML文件并创建控制器实例时,它将FXML文件中与fx:id属性匹配的元素注入带@FXML注释的字段,然后调用initialize()方法。

您的FXMLpopUp1.fxml似乎没有任何具有fx:id="customersList"的元素,因此customersList在该控制器中保持为空。因此,当initialize()方法调用setListView()时,你会得到一个空指针异常。

应该为每个FXML文件使用不同的控制器类。FXMLpopUp1.fxml的控制器类可能看起来像

public class PopupController {
    @FXML
    private Button confirmPop ;
    private boolean confirmed = false ;
    public void isConfirmed() {
        return confirmed ;
    }
    @FXML
    private void confirmPop() {
        confirmed = true ;
        confirmPop.getScene().getWindow().hide();
    }
    @FXML
    private void cancelPop() {
        confirmed = false ;
        confirmPop.getScene().getWindow().hide();
    }
}

然后在主控制器中输入

@FXML
private void addCustomer(ActionEvent event) throws IOException {
    Stage stage;
    Parent root;
    stage = new Stage();
    FXMLLoader loader = new FXMLLoader(getClass().getResource("FXMLpopUp1.fxml"));
    root = loader.load();
    PopupController popupController = loader.getController();
    stage.setScene(new Scene(root));
    stage.initModality(Modality.APPLICATION_MODAL);
    stage.initOwner(addCustomerButton.getScene().getWindow());
    stage.showAndWait();
    if (popupController.isConfirmed()) {
        // confirm button was pressed...
    }
}

相关内容

  • 没有找到相关文章

最新更新