I am getting a NullPointerException
although I am giving the values. I have a DefaultTableModel
called model1
. I want to multiply the values of column 4 and 5 and add it to column 6. I made three rows. The value at 4,5 of the first two rows gets multiplied and added on column 6 but the value of the third row doesn't display on the column and generates an exception.
Here's the error block-
for(int i = 0; i <= model1.getRowCount();i++){
Double d = Double.parseDouble((String) model1.getValueAt(i, 4));
Double d2 = Double.parseDouble((String) model1.getValueAt(i, 5)); //exception
Double d3 = d * d2;
model1.setValueAt(d3, i, 6);
}
Here's the exception-
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1838)
at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
at java.lang.Double.parseDouble(Double.java:538)
at Busy.saver(Busy.java:246)
at Busy.lambda$inventory$12(Busy.java:233)
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:6533)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6298)
at java.awt.Container.processEvent(Container.java:2237)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2295)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4889)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4526)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4467)
at java.awt.Container.dispatchEventImpl(Container.java:2281)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4711)
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:80)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
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:80)
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)
BUILD SUCCESSFUL (total time: 48 seconds)
Why am I getting this exception?
Here's the MCVE-
import java.awt.*;
import java.io.Serializable;
import javax.swing.*;
import javax.swing.table.*;
public class Mcve extends JPanel{
String data[][] ={{""}};
String row[] = {"#","Item Name","HSN/SAC","Description","Qty.","Rate","Price"};
DefaultTableModel model = new DefaultTableModel(data, row);
JTable table = new JTable(model);
JScrollPane pane = new JScrollPane(table);
public void mainPage() {
Mcve obj = new Mcve();
JFrame frame = new JFrame("MCVE");
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
JInternalFrame iframe = new JInternalFrame();
JButton save , add;
save = new JButton("Save");
add = new JButton("Add Item");
save.setSize(75,40);
add.setSize(85,40);
iframe.setTitle("Inventory");
iframe.add(pane);
iframe.add(save, BorderLayout.WEST);
iframe.add(add, BorderLayout.SOUTH);
frame.add(iframe);
iframe.setClosable(true);
save.setVisible(true);
add.setVisible(true);
iframe.setVisible(true);
frame.setVisible(true);
save.addActionListener((ActionEvent) ->{
saver();
});
add.addActionListener((ActionEvent) -> {
rowAdder();
});
}
public void saver(){
for(int i = 0; i<=model.getRowCount();i++){
Double d= Double.parseDouble((String) model.getValueAt(i,4));
Double d2= Double.parseDouble((String) model.getValueAt(i,5));
Double d3=d*d2;
model.setValueAt(d3,i,6);
}
}
public void rowAdder(){
String row[][] = {};
model.addRow(row);
}
public static void main(String[] args) {
Mcve obj = new Mcve();
obj.mainPage();
}
}