当我尝试在javafx中显示一个Dialog时,我得到nullpointerexception

时间:2016-10-20 15:54:48

标签: java javafx dialog modal-dialog

我有一个按钮addProduct,它通过AddProducts方法获取一个动作来显示一个Dialog,以便向表中添加项目但是当我触发它时会出现很多错误,当我尝试显示其他对话框时,我会遇到同样的问题将其他项添加到其他表中。

 addProduct.setOnAction(event -> {
        try {
            AddProducts(table);
        } catch (IOException e) {
            e.printStackTrace();
        }
    });

我有一个方法AddProducts,当我点击addProduct时它应该显示一个Dialog但它会引发这个异常。

Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at com.sun.javafx.scene.control.skin.Utils.computeTextWidth(Utils.java:118)
at com.sun.javafx.scene.control.skin.LabeledSkinBase.computeMinLabeledPartWidth(LabeledSkinBase.java:661)
at com.sun.javafx.scene.control.skin.LabeledSkinBase.computeMinWidth(LabeledSkinBase.java:633)
at javafx.scene.control.Control.computeMinWidth(Control.java:470)
at javafx.scene.Parent.minWidth(Parent.java:943)
at javafx.scene.layout.Region.minWidth(Region.java:1383)
at javafx.scene.layout.Region.computeChildPrefAreaWidth(Region.java:1728)
at javafx.scene.layout.GridPane.computePrefWidths(GridPane.java:1556)
at javafx.scene.layout.GridPane.computePrefWidth(GridPane.java:1242)
at javafx.scene.Parent.prefWidth(Parent.java:915)
at javafx.scene.layout.Region.prefWidth(Region.java:1419)
at javafx.scene.control.DialogPane.computePrefWidth(DialogPane.java:973)
at javafx.scene.Parent.prefWidth(Parent.java:915)
at javafx.scene.layout.Region.prefWidth(Region.java:1419)
at javafx.scene.Scene.getPreferredWidth(Scene.java:1697)
at javafx.scene.Scene.resizeRootToPreferredSize(Scene.java:1673)
at javafx.scene.Scene.preferredSize(Scene.java:1645)
at javafx.scene.Scene.impl_preferredSize(Scene.java:1720)
at javafx.stage.Window$9.invalidated(Window.java:846)
at javafx.beans.property.BooleanPropertyBase.markInvalid(BooleanPropertyBase.java:109)
at javafx.beans.property.BooleanPropertyBase.set(BooleanPropertyBase.java:144)
at javafx.stage.Window.setShowing(Window.java:922)
at javafx.stage.Window.show(Window.java:937)
at javafx.stage.Stage.show(Stage.java:259)
at javafx.stage.Stage.showAndWait(Stage.java:472)
at javafx.scene.control.HeavyweightDialog.showAndWait(HeavyweightDialog.java:162)
at javafx.scene.control.Dialog.showAndWait(Dialog.java:341)
**at ddapp.TabsView.AddProducts(TabsView.java:825)
at ddapp.TabsView.lambda$ShowProductsTable$5(TabsView.java:432)**
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.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.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:380)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:294)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:416)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:415)
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.gtk.GtkApplication._runLoop(Native Method)
at com.sun.glass.ui.gtk.GtkApplication.lambda$null$49(GtkApplication.java:139)
at java.lang.Thread.run(Thread.java:745)

这是方法AddProducts:

public void AddProducts(final TableView<ProductsValues> table) throws IOException {
    Dialog<ProductsValues> addP = new Dialog<>();
    DialogPane dialogPane = addP.getDialogPane();
    dialogPane.getStylesheets().add(getClass().getClassLoader().getResource("JMetroLightTheme.css").toExternalForm());
    addP.setTitle("Añadir Producto");
    addP.setHeaderText("Ingrese la información correspondiente al producto que desea añadir al inventario.");
    addP.setResizable(false);
    addP.initModality(APPLICATION_MODAL);
    addP.initStyle(StageStyle.TRANSPARENT);

    Label label2 = new Label(CODE);
    label2.setFont(fontAwesome);
    label2.setTextFill(Color.web("#4E0A07"));
    JFXTextField text2 = new JFXTextField();
    text2.setPromptText("Codigo Interno");
    text2.setLabelFloat(true);
    Label label1 = new Label(BARCODE);
    label1.setFont(fontAwesome);
    label1.setTextFill(Color.web("#4E0A07"));
    JFXTextField text1 = new JFXTextField();
    text1.setPromptText("Codigo de Barra");
    text1.setLabelFloat(true);
    Label label3 = new Label(INFO);
    label3.setFont(fontAwesome);
    label3.setTextFill(Color.web("#4E0A07"));
    JFXTextField text3 = new JFXTextField();
    text3.setPromptText("Descripción");
    text3.setLabelFloat(true);
    Label label4 = new Label(MARK);
    label4.setFont(fontAwesome);
    label4.setTextFill(Color.web("#4E0A07"));
    JFXTextField text4 = new JFXTextField();
    text4.setPromptText("Marca");
    text4.setLabelFloat(true);
    Label label5 = new Label(PACK);
    label5.setFont(fontAwesome);
    label5.setTextFill(Color.web("#4E0A07"));
    JFXTextField text5 = new JFXTextField();
    text5.setPromptText("Embalaje");
    text5.setLabelFloat(true);
    Label label6 = new Label(BOX);
    label6.setFont(fontAwesome);
    label6.setTextFill(Color.web("#4E0A07"));
    JFXTextField text6 = new JFXTextField();
    text6.setPromptText("Unidades por Paquete");
    text6.setLabelFloat(true);
    Label label7 = new Label(PRODUCT);
    label7.setFont(fontAwesome);
    label7.setTextFill(Color.web("#4E0A07"));
    JFXTextField text7 = new JFXTextField();
    text7.setPromptText("Precio Unitario");
    text7.setLabelFloat(true);
    Label label8 = new Label(USD);
    label8.setFont(fontAwesome);
    label8.setTextFill(Color.web("#4E0A07"));
    JFXTextField text8 = new JFXTextField();
    text8.setPromptText("Precio Paquete");
    text8.setLabelFloat(true);

    GridPane grid = new GridPane();
    grid.add(label2, 1, 1);
    grid.add(text2, 2, 1);
    grid.setMargin(text2, new Insets(15, 0, 0, 0));
    grid.setMargin(label2, new Insets(15, 0, 0, 0));
    grid.add(label1, 1, 2);
    grid.add(text1, 2, 2);
    grid.setMargin(text1, new Insets(15, 0, 0, 0));
    grid.setMargin(label1, new Insets(15, 0, 0, 0));

    grid.add(label3, 1, 3);
    grid.add(text3, 2, 3);
    grid.setMargin(text3, new Insets(15, 0, 0, 0));
    grid.setMargin(label3, new Insets(15, 0, 0, 0));
    grid.add(label4, 1, 4);
    grid.add(text4, 2, 4);
    grid.setMargin(text4, new Insets(15, 0, 0, 0));
    grid.setMargin(label4, new Insets(15, 0, 0, 0));
    grid.add(label5, 1, 5);
    grid.add(text5, 2, 5);
    grid.setMargin(text5, new Insets(15, 0, 0, 0));
    grid.setMargin(label5, new Insets(15, 0, 0, 0));
    grid.add(label6, 1, 6);
    grid.add(text6, 2, 6);
    grid.setMargin(text6, new Insets(15, 0, 0, 0));
    grid.setMargin(label6, new Insets(15, 0, 0, 0));
    grid.add(label7, 1, 7);
    grid.add(text7, 2, 7);
    grid.setMargin(text7, new Insets(15, 0, 0, 0));
    grid.setMargin(label7, new Insets(15, 0, 0, 0));
    grid.add(label8, 1, 8);
    grid.add(text8, 2, 8);
    grid.setMargin(text8, new Insets(15, 0, 0, 0));
    grid.setMargin(label8, new Insets(15, 0, 0, 0));
    addP.getDialogPane().setContent(grid);
    addP.initOwner(mainStage);
    ButtonType buttonTypeOk = new ButtonType("Agregar", ButtonBar.ButtonData.OK_DONE);
    addP.getDialogPane().getButtonTypes().add(buttonTypeOk);
    ButtonType buttonTypeCancel = new ButtonType("Cancelar", ButtonBar.ButtonData.CANCEL_CLOSE);
    addP.getDialogPane().getButtonTypes().add(buttonTypeCancel);


    addP.setResultConverter(b -> {
        if (b == buttonTypeOk) {
            int nextIndex = table.getSelectionModel().getSelectedIndex();
            table.getItems().add(nextIndex, new ProductsValues(text1.getText(), text2.getText(), text3.getText(), text4.getText(), text5.getText(), Integer.parseInt(text6.getText()), Double.parseDouble(text7.getText()), Double.parseDouble(text8.getText())));
            table.getSelectionModel().select(nextIndex);
            addP.close();
            return new ProductsValues(text2.getText(), text1.getText(), text3.getText(), text4.getText(), text5.getText(), Integer.parseInt(text6.getText()), Double.parseDouble(text7.getText()), Double.parseDouble(text8.getText()));
        } else
            addP.close();
        return null;
    });
    addP.showAndWait();
}

这是我的课程中的一行,它认为是错误:

addP.showAndWait();//825, when I show the Dialog
AddProducts(table);//432, when I call the method

0 个答案:

没有答案