I have data in jTable1. I attempted to send them to jTable2 , after calculating by value entered to JOption Pane. But when I clicked on cancelled button on JOption Pane program making error. Line 80 in my code is
BigDecimal quantity = new BigDecimal(JOptionPane.showInputDialog(null,"Enter Quantity", 0));
I need help to solve this problem. Thanks in advance.
Code is shown below
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;
}
Stack Trace shown below
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$000(TableGUI.java:7)
at soquestion.TableGUI$1.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.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.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$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.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)
CodePudding user response:
I changed my code according to the given opinions. It works well now. Thnaks all who helped me. @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");
}