使用 JOption Pane 时,在 Canceled JOption Pane 期间,发生 NullPointerException

When using JOption Pane, During Cancelled JOption Pane, NullPointerException occurs

我在 jTable1 中有数据。在按输入到 JOption Pane 的值进行计算后,我试图将它们发送到 jTable2 。但是当我单击 JOption Pane 程序上的取消按钮时出错。 我代码中的第 80 行是

BigDecimal quantity = new BigDecimal(JOptionPane.showInputDialog(null,"Enter Quantity", 0));

我需要帮助来解决这个问题。提前致谢。

代码如下

    import java.math.BigDecimal;
    import javax.swing.JOptionPane;
    import javax.swing.table.DefaultTableModel;

    public class TableGUI extends javax.swing.JFrame {

    public TableGUI() {
        initComponents();
    }

    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(new java.awt.GridBagLayout());

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
                new Object[][]{
                    {"1", "Cement", "cwt", "1050.00", "1.00", "2.00", "2100.00"},
                    {"2", "Sand", "cube", "7500.00", "1.00", "1.50", "11250.00"},
                    {"3", "Skill labour", "Day", "2500.00", "1.00", "2.50", "6250.00"},
                    {"4", "Unskill labour", "Day", "1500.00", "1.00", "2.50", "3750.00"}
                },
                new String[]{
                    "ID", "Name", "Unit", "Rate", "Usage", "Quantity", "Amount"
                }
        ));
        jTable1.setRowHeight(25);
        jScrollPane1.setViewportView(jTable1);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 0.1;
        gridBagConstraints.weighty = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        getContentPane().add(jScrollPane1, gridBagConstraints);

        jTable2.setModel(new javax.swing.table.DefaultTableModel(
                new Object[][]{},
                new String[]{
                    "ID", "Name", "Unit", "Rate", "Usage", "Quantity", "Amount"
                }
        ));
        jTable2.setRowHeight(25);
        jScrollPane2.setViewportView(jTable2);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 0.1;
        gridBagConstraints.weighty = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        getContentPane().add(jScrollPane2, gridBagConstraints);

        jButton1.setText("Calculate");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        getContentPane().add(jButton1, gridBagConstraints);

        pack();
    }

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

        BigDecimal quantity = new BigDecimal(JOptionPane.showInputDialog(null,"Enter Quantity", 
     0));

        if (quantity != null && quantity.equals("")) {
            
            JOptionPane.showMessageDialog(null, "Calculation Cancelled");
            
        } else {
            DefaultTableModel model1 = (DefaultTableModel) jTable1.getModel();

            for (int i = 0; i < model1.getRowCount(); i++) {

    Object data1 = jTable1.getValueAt(i, 0);
    Object data2 = jTable1.getValueAt(i, 1);
    Object data3 = jTable1.getValueAt(i, 2);
    BigDecimal data4 = new BigDecimal((String) jTable1.getValueAt(i, 3).toString());
    BigDecimal data5 = new BigDecimal((String) jTable1.getValueAt(i, 4).toString());
    BigDecimal data6 = quantity.multiply(new BigDecimal((String) 
    jTable1.getValueAt(i,5).toString()));
    data6 = data6.setScale(2, BigDecimal.ROUND_HALF_UP);
    BigDecimal data7 = data4.multiply(data5.multiply(data6));
    data7 = data7.setScale(2, BigDecimal.ROUND_HALF_UP);

                Object[] row = {data1, data2, data3, data4, data5, data6, data7};
                DefaultTableModel model2 = (DefaultTableModel) jTable2.getModel();
                model2.addRow(row);
            }
        }
    }

    public static void main(String args[]) {

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new TableGUI().setVisible(true);
            }
        });
    }

    private javax.swing.JButton jButton1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    private javax.swing.JTable jTable2;

   }

堆栈跟踪如下所示

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at java.math.BigDecimal.<init>(BigDecimal.java:806)
at soquestion.TableGUI.jButton1ActionPerformed(TableGUI.java:80)
at soquestion.TableGUI.access[=12=]0(TableGUI.java:7)
at soquestion.TableGUI.actionPerformed(TableGUI.java:65)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6535)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6300)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4891)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4713)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2750)
at java.awt.Component.dispatchEvent(Component.java:4713)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access0(EventQueue.java:97)
at java.awt.EventQueue.run(EventQueue.java:709)
at java.awt.EventQueue.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue.run(EventQueue.java:731)
at java.awt.EventQueue.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

我根据给定的意见修改了我的代码。现在效果很好。感谢所有帮助过我的人。 @QBrute,@Pshemo,@ControlAltDel

  String userValue = JOptionPane.showInputDialog("Enter Quantity");

    if (userValue != null && !userValue.isEmpty()) {

        BigDecimal quantity = new BigDecimal(userValue);

        DefaultTableModel model1 = (DefaultTableModel) jTable1.getModel();

        for (int i = 0; i < model1.getRowCount(); i++) {

            Object data1 = jTable1.getValueAt(i, 0);
            Object data2 = jTable1.getValueAt(i, 1);
            Object data3 = jTable1.getValueAt(i, 2);
            BigDecimal data4 = new BigDecimal((String) jTable1.getValueAt(i, 3).toString());
            BigDecimal data5 = new BigDecimal((String) jTable1.getValueAt(i, 4).toString());
            BigDecimal data6 = quantity.multiply(new BigDecimal((String) jTable1.getValueAt(i, 5).toString()));
            data6 = data6.setScale(2, BigDecimal.ROUND_HALF_UP);
            BigDecimal data7 = data4.multiply(data5.multiply(data6));
            data7 = data7.setScale(2, BigDecimal.ROUND_HALF_UP);

            Object[] row = {data1, data2, data3, data4, data5, data6, data7};
            DefaultTableModel model2 = (DefaultTableModel) jTable2.getModel();
            model2.addRow(row);
        }

    } else {
        JOptionPane.showMessageDialog(null, "Calculation Cancelled");
    }