我尝试实现自己的TableModel
但是在调用NullPointerException
方法时我一直收到getRowCount
异常,但相关的数据字段已经启动。我有点困惑如何发生这种情况。
public class TransactionsTableModel extends AbstractTableModel {
private static final long serialVersionUID = 527329780735382668L;
private static final int DESCRIPTION_INDEX = 0;
private static final int VALUE_INDEX = 1;
private List<String> columnNames;
private List<NatwestTransaction> data = new ArrayList<NatwestTransaction>();
public TransactionsTableModel() {
this.columnNames = new ArrayList<String>();
this.columnNames.add("Description");
this.columnNames.add("Value");
}
public void updateTable(List<NatwestTransaction> list) {
data = list;
fireTableDataChanged();
}
@Override
public int getRowCount() {
return data.size(); // line 31 - error here
}
@Override
public int getColumnCount() {
return columnNames.size();
}
@Override
public String getColumnName(int index) {
return columnNames.get(index);
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
NatwestTransaction t = data.get(rowIndex);
switch(columnIndex) {
case DESCRIPTION_INDEX:
return t.getDescription();
case VALUE_INDEX:
return t.getValue();
default:
return new Object();
}
}
@SuppressWarnings({ "unchecked", "rawtypes" })
public Class getColumnClass(int c) {
switch(c) {
case DESCRIPTION_INDEX:
return String.class;
case VALUE_INDEX:
return Float.class;
default:
return Object.class;
}
}
}
这是控制台输出:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Transactions.TransactionsTableModel.getRowCount(TransactionsTableModel.java:31)
at javax.swing.JTable.getRowCount(JTable.java:2662)
at javax.swing.plaf.basic.BasicTableUI.paint(BasicTableUI.java:1766)
at javax.swing.plaf.ComponentUI.update(ComponentUI.java:161)
at javax.swing.JComponent.paintComponent(JComponent.java:778)
at javax.swing.JComponent.paint(JComponent.java:1054)
at javax.swing.JComponent.paintChildren(JComponent.java:887)
at javax.swing.JComponent.paint(JComponent.java:1063)
at javax.swing.JViewport.paint(JViewport.java:731)
at javax.swing.JComponent.paintChildren(JComponent.java:887)
at javax.swing.JComponent.paint(JComponent.java:1063)
at javax.swing.JComponent.paintChildren(JComponent.java:887)
at javax.swing.JComponent.paint(JComponent.java:1063)
at javax.swing.JComponent.paintChildren(JComponent.java:887)
at javax.swing.JComponent.paint(JComponent.java:1063)
at javax.swing.JComponent.paintChildren(JComponent.java:887)
at javax.swing.JComponent.paint(JComponent.java:1063)
at javax.swing.JComponent.paintChildren(JComponent.java:887)
at javax.swing.JComponent.paint(JComponent.java:1063)
at javax.swing.JLayeredPane.paint(JLayeredPane.java:585)
at javax.swing.JComponent.paintChildren(JComponent.java:887)
at javax.swing.JComponent.paint(JComponent.java:1063)
at javax.swing.JComponent.paintToOffscreen(JComponent.java:5219)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1529)
at javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1452)
at javax.swing.RepaintManager.paint(RepaintManager.java:1249)
at javax.swing.JComponent._paintImmediately(JComponent.java:5167)
at javax.swing.JComponent.paintImmediately(JComponent.java:4978)
at javax.swing.RepaintManager$3.run(RepaintManager.java:808)
at javax.swing.RepaintManager$3.run(RepaintManager.java:796)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:796)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:769)
at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:718)
at javax.swing.RepaintManager.access$1100(RepaintManager.java:62)
at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1677)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:703)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Transactions.TransactionsTableModel.getRowCount(TransactionsTableModel.java:31)
at javax.swing.JTable.getRowCount(JTable.java:2662)
at javax.swing.plaf.basic.BasicTableUI.createTableSize(BasicTableUI.java:1692)
at javax.swing.plaf.basic.BasicTableUI.getPreferredSize(BasicTableUI.java:1733)
at javax.swing.JComponent.getPreferredSize(JComponent.java:1660)
at javax.swing.ScrollPaneLayout.layoutContainer(ScrollPaneLayout.java:790)
at java.awt.Container.layout(Container.java:1503)
at java.awt.Container.doLayout(Container.java:1492)
at java.awt.Container.validateTree(Container.java:1688)
at java.awt.Container.validate(Container.java:1623)
at javax.swing.RepaintManager$2.run(RepaintManager.java:691)
at javax.swing.RepaintManager$2.run(RepaintManager.java:689)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at javax.swing.RepaintManager.validateInvalidComponents(RepaintManager.java:688)
at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1676)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:703)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
答案 0 :(得分:1)
NullPointerException
发生在data.size()
,因为您尝试在对象size()
上调用函数data
,但很可能data
为{{1}并且不引用null
对象。
可能发生这种情况的原因是使用null参数调用List
函数,即代码的某些部分正在执行updateTable(List<NatwestTransaction>)
,导致第25行的transactionsTableModelObject.updateTable(null)
为执行为data = list
,这将覆盖data = null
的先前(初始化)值,从而导致问题。