为什么我会在这一行收到 NullPointerException?
Why am I getting NullPointerException at this line?
虽然我给出了值,但我得到了 NullPointerException
。我有一个名为 model1
的 DefaultTableModel
。我想将第 4 列和第 5 列的值相乘并将其添加到第 6 列。我做了三行。前两行 4,5 处的值在第 6 列上相乘并相加,但第三行的值未显示在该列上并生成异常。
这是错误块-
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);
}
这里是例外-
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(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.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:80)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
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: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)
为什么我会收到此异常?
这是 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();
}
}
列是零索引的。所以第 4 列和第 5 列是 索引 3 和 4。
如果您的第六列是结果的占位符,则它可能是空的,因此错误地调用 model1.getValueAt(i, 5)
会返回 null
,因为该单元格是空的。
Double d = Double.parseDouble((String) model1.getValueAt(i, 3));
Double d2 = Double.parseDouble((String) model1.getValueAt(i, 4));
Double d3 = d * d2;
model1.setValueAt(d3, i, 5);
仅使用 Stacktrace 和文档:
我们可以看到这是Double.parseDouble
的调用抛出了异常
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)
Double.parseDouble 的文档告诉你:
Throws:
- NullPointerException - if the string is null
因此您将 null
值传递给 Double.parseString
。
我的原因是当你点击按钮保存时你仍然处于 "edit" 模式,你不会提交值所以单元格仍然被模型视为空(null) .你可以在saver
方法的开头强制版本结束。
TableCellEditor editor = table.getCellEditor();
if (editor != null) {
editor.stopCellEditing();
}
来自 Guillaume answer.
当然,如果单元格不是数值(空或非数字),这不会阻止异常。所以解决方案是捕获每一行的异常。
for (int i = 0; i <= model.getRowCount(); i++) {
try{
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);
} catch (NullPointerException | NumberFormatException e){
model.setValueAt("N/A", i, 6);
}
}
然后,您可以检查 以更正您将从您正在使用的循环中获得的 java.lang.ArrayIndexOutOfBoundsException
。
for(int i = 0; i<=model1.getRowCount();i++)
你循环一次太多了。你需要去 i<model1.getRowCount())
正如 AxelH 在他的评论中提到的,这不会解决您的 NPE 问题。但这仍然是您的代码中的一个错误,一旦您解决了 NPE,就会导致问题。
虽然我给出了值,但我得到了 NullPointerException
。我有一个名为 model1
的 DefaultTableModel
。我想将第 4 列和第 5 列的值相乘并将其添加到第 6 列。我做了三行。前两行 4,5 处的值在第 6 列上相乘并相加,但第三行的值未显示在该列上并生成异常。
这是错误块-
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);
}
这里是例外-
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(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.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:80)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
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: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)
为什么我会收到此异常?
这是 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();
}
}
列是零索引的。所以第 4 列和第 5 列是 索引 3 和 4。
如果您的第六列是结果的占位符,则它可能是空的,因此错误地调用 model1.getValueAt(i, 5)
会返回 null
,因为该单元格是空的。
Double d = Double.parseDouble((String) model1.getValueAt(i, 3));
Double d2 = Double.parseDouble((String) model1.getValueAt(i, 4));
Double d3 = d * d2;
model1.setValueAt(d3, i, 5);
仅使用 Stacktrace 和文档:
我们可以看到这是Double.parseDouble
的调用抛出了异常
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)
Double.parseDouble 的文档告诉你:
Throws:
- NullPointerException - if the string is null
因此您将 null
值传递给 Double.parseString
。
我的原因是当你点击按钮保存时你仍然处于 "edit" 模式,你不会提交值所以单元格仍然被模型视为空(null) .你可以在saver
方法的开头强制版本结束。
TableCellEditor editor = table.getCellEditor();
if (editor != null) {
editor.stopCellEditing();
}
来自 Guillaume answer.
当然,如果单元格不是数值(空或非数字),这不会阻止异常。所以解决方案是捕获每一行的异常。
for (int i = 0; i <= model.getRowCount(); i++) {
try{
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);
} catch (NullPointerException | NumberFormatException e){
model.setValueAt("N/A", i, 6);
}
}
然后,您可以检查 java.lang.ArrayIndexOutOfBoundsException
。
for(int i = 0; i<=model1.getRowCount();i++)
你循环一次太多了。你需要去 i<model1.getRowCount())
正如 AxelH 在他的评论中提到的,这不会解决您的 NPE 问题。但这仍然是您的代码中的一个错误,一旦您解决了 NPE,就会导致问题。