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

时间:2016-10-25 19:50:48

标签: java listview javafx event-handling

嘿,这是我正在为学校写作的程序的一部分,并且为了一些共鸣,我的弹出窗口“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

1 个答案:

答案 0 :(得分:0)

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

FXMLLoader加载FXML文件并创建控制器实例时,它会注释注释@FXML的字段,其中FXML文件中的元素具有匹配的fx:id属性,然后调用{{ 1}}方法。

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

您应该为每个FXML文件使用不同的控制器类。 setListView()的控制器类可能类似于

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();
    }
}