I am getting an odd null pointer. This is my TableModel:
private class Model extends AbstractTableModel implements IEventRespondedToListener{
private List<ModelPojo> elements;
private String[] columnNames = new String[]{
"Class",
"Event",
"Event Id",
"Time"
};
public void Model(){
elements = new ArrayList<>();
}
@Override
public int getRowCount() {
return elements.size();
}
...
It is created like so:
public UpdateWindow(UpdateManager updateManager){
this.updateManager = updateManager;
model = new Model();
However, the exception I get is a null pointer:
java.lang.NullPointerException
at com.dvsd.profmetric.jms.UpdateWindow$Model.getRowCount(UpdateWindow.java:60)
at javax.swing.JTable.getRowCount(JTable.java:2664)
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:1659)
at javax.swing.ScrollPaneLayout.preferredLayoutSize(ScrollPaneLayout.java:495)
at java.awt.Container.preferredSize(Container.java:1794)
Which points to this line:
return elements.size();
I dont understand how this can be null if elements
is created inside the constructor. Any ideas?
This
public void Model(){
should probably be
public Model(){
The addition of void
makes it a void method and not a constructor. So when you use public Model()
, you call the implicit default constructor (assuming there is no other constructor) where elements
is not given a value and therefore remains null.