NPE with initialized variable [duplicate]
Clash Royale CLAN TAG#URR8PPP
up vote
14
down vote
favorite
This question already has an answer here:
What's wrong with overridable method calls in constructors?
7 answers
Can someone explain to me why I am getting a NullPointerException in the getRowCount() method? The variable is initialized with an empty ArrayList...
public class BeschriftungssetTableModel extends DefaultTableModel
private static final long serialVersionUID = -4980235976337188354L;
private List<BeschriftungssetBean> data = new ArrayList<>();
public void setData(List<BeschriftungssetBean> data)
this.data = data;
@Override
public int getColumnCount()
return 1;
@Override
public int getRowCount()
return data.size();
@Override
public Object getValueAt(int row, int column)
return data.get(row).getBezeichnung();
@Override
public String getColumnName(int column)
return "Bezeichnung";
public static void main(String args)
BeschriftungssetTableModel beschriftungssetTableModel = new BeschriftungssetTableModel();
beschriftungssetTableModel.getRowCount();
public class BeschriftungssetBean
private String objId;
private String bezeichnung;
public String getBezeichnung()
return bezeichnung;
public void setBezeichnung(String bezeichnung)
this.bezeichnung = bezeichnung;
public String getObjId()
return objId;
public void setObjId(String objId)
this.objId = objId;
Exception in thread "main" java.lang.NullPointerException
at ch.aaa.xxx.yyy.gruppen.plugin.anzeige.beschriftungseinstellungen.BeschriftungssetTableModel.getRowCount(BeschriftungssetTableModel.java:36)
at javax.swing.table.DefaultTableModel.setDataVector(DefaultTableModel.java:224)
at javax.swing.table.DefaultTableModel.<init>(DefaultTableModel.java:124)
at javax.swing.table.DefaultTableModel.<init>(DefaultTableModel.java:106)
at javax.swing.table.DefaultTableModel.<init>(DefaultTableModel.java:86)
at ch.aaa.xxx.yyy.gruppen.plugin.anzeige.beschriftungseinstellungen.BeschriftungssetTableModel.<init>(BeschriftungssetTableModel.java:18)
at ch.aaa.xxx.yyy.gruppen.plugin.anzeige.beschriftungseinstellungen.BeschriftungssetTableModel.main(BeschriftungssetTableModel.java:50)
java swing
marked as duplicate by Eugene
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Aug 17 at 19:24
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
 |Â
show 9 more comments
up vote
14
down vote
favorite
This question already has an answer here:
What's wrong with overridable method calls in constructors?
7 answers
Can someone explain to me why I am getting a NullPointerException in the getRowCount() method? The variable is initialized with an empty ArrayList...
public class BeschriftungssetTableModel extends DefaultTableModel
private static final long serialVersionUID = -4980235976337188354L;
private List<BeschriftungssetBean> data = new ArrayList<>();
public void setData(List<BeschriftungssetBean> data)
this.data = data;
@Override
public int getColumnCount()
return 1;
@Override
public int getRowCount()
return data.size();
@Override
public Object getValueAt(int row, int column)
return data.get(row).getBezeichnung();
@Override
public String getColumnName(int column)
return "Bezeichnung";
public static void main(String args)
BeschriftungssetTableModel beschriftungssetTableModel = new BeschriftungssetTableModel();
beschriftungssetTableModel.getRowCount();
public class BeschriftungssetBean
private String objId;
private String bezeichnung;
public String getBezeichnung()
return bezeichnung;
public void setBezeichnung(String bezeichnung)
this.bezeichnung = bezeichnung;
public String getObjId()
return objId;
public void setObjId(String objId)
this.objId = objId;
Exception in thread "main" java.lang.NullPointerException
at ch.aaa.xxx.yyy.gruppen.plugin.anzeige.beschriftungseinstellungen.BeschriftungssetTableModel.getRowCount(BeschriftungssetTableModel.java:36)
at javax.swing.table.DefaultTableModel.setDataVector(DefaultTableModel.java:224)
at javax.swing.table.DefaultTableModel.<init>(DefaultTableModel.java:124)
at javax.swing.table.DefaultTableModel.<init>(DefaultTableModel.java:106)
at javax.swing.table.DefaultTableModel.<init>(DefaultTableModel.java:86)
at ch.aaa.xxx.yyy.gruppen.plugin.anzeige.beschriftungseinstellungen.BeschriftungssetTableModel.<init>(BeschriftungssetTableModel.java:18)
at ch.aaa.xxx.yyy.gruppen.plugin.anzeige.beschriftungseinstellungen.BeschriftungssetTableModel.main(BeschriftungssetTableModel.java:50)
java swing
marked as duplicate by Eugene
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Aug 17 at 19:24
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
3
Could you add the stack trace for theNullPointerException
?
â deHaar
Aug 17 at 7:14
5
If your plan is to replace everything DefaultTableModel does by your own methods, and to use your own list rather than the data that DefaultTableModel uses, implement AbstractTableModel instead of extending DefaultTableModel.
â JB Nizet
Aug 17 at 7:29
3
Unrelated: consider not mixing languages in your code. And as BeschriftungsMengeBohne sounds really bad, I would rather go all EnglishInscriptionSetBean
for example.
â GhostCat
Aug 17 at 7:41
1
I would definately go with @JBNizet 's advice and extend AbstractTableModel
â keuleJ
Aug 17 at 8:06
1
@GuntramBlohm well it's complicated enough to know all the special terms that exist in german... let alone finding the correct translation... there would be absolutely no benefits for our projects since speaking german is a requirement
â user489872
Aug 18 at 15:21
 |Â
show 9 more comments
up vote
14
down vote
favorite
up vote
14
down vote
favorite
This question already has an answer here:
What's wrong with overridable method calls in constructors?
7 answers
Can someone explain to me why I am getting a NullPointerException in the getRowCount() method? The variable is initialized with an empty ArrayList...
public class BeschriftungssetTableModel extends DefaultTableModel
private static final long serialVersionUID = -4980235976337188354L;
private List<BeschriftungssetBean> data = new ArrayList<>();
public void setData(List<BeschriftungssetBean> data)
this.data = data;
@Override
public int getColumnCount()
return 1;
@Override
public int getRowCount()
return data.size();
@Override
public Object getValueAt(int row, int column)
return data.get(row).getBezeichnung();
@Override
public String getColumnName(int column)
return "Bezeichnung";
public static void main(String args)
BeschriftungssetTableModel beschriftungssetTableModel = new BeschriftungssetTableModel();
beschriftungssetTableModel.getRowCount();
public class BeschriftungssetBean
private String objId;
private String bezeichnung;
public String getBezeichnung()
return bezeichnung;
public void setBezeichnung(String bezeichnung)
this.bezeichnung = bezeichnung;
public String getObjId()
return objId;
public void setObjId(String objId)
this.objId = objId;
Exception in thread "main" java.lang.NullPointerException
at ch.aaa.xxx.yyy.gruppen.plugin.anzeige.beschriftungseinstellungen.BeschriftungssetTableModel.getRowCount(BeschriftungssetTableModel.java:36)
at javax.swing.table.DefaultTableModel.setDataVector(DefaultTableModel.java:224)
at javax.swing.table.DefaultTableModel.<init>(DefaultTableModel.java:124)
at javax.swing.table.DefaultTableModel.<init>(DefaultTableModel.java:106)
at javax.swing.table.DefaultTableModel.<init>(DefaultTableModel.java:86)
at ch.aaa.xxx.yyy.gruppen.plugin.anzeige.beschriftungseinstellungen.BeschriftungssetTableModel.<init>(BeschriftungssetTableModel.java:18)
at ch.aaa.xxx.yyy.gruppen.plugin.anzeige.beschriftungseinstellungen.BeschriftungssetTableModel.main(BeschriftungssetTableModel.java:50)
java swing
This question already has an answer here:
What's wrong with overridable method calls in constructors?
7 answers
Can someone explain to me why I am getting a NullPointerException in the getRowCount() method? The variable is initialized with an empty ArrayList...
public class BeschriftungssetTableModel extends DefaultTableModel
private static final long serialVersionUID = -4980235976337188354L;
private List<BeschriftungssetBean> data = new ArrayList<>();
public void setData(List<BeschriftungssetBean> data)
this.data = data;
@Override
public int getColumnCount()
return 1;
@Override
public int getRowCount()
return data.size();
@Override
public Object getValueAt(int row, int column)
return data.get(row).getBezeichnung();
@Override
public String getColumnName(int column)
return "Bezeichnung";
public static void main(String args)
BeschriftungssetTableModel beschriftungssetTableModel = new BeschriftungssetTableModel();
beschriftungssetTableModel.getRowCount();
public class BeschriftungssetBean
private String objId;
private String bezeichnung;
public String getBezeichnung()
return bezeichnung;
public void setBezeichnung(String bezeichnung)
this.bezeichnung = bezeichnung;
public String getObjId()
return objId;
public void setObjId(String objId)
this.objId = objId;
Exception in thread "main" java.lang.NullPointerException
at ch.aaa.xxx.yyy.gruppen.plugin.anzeige.beschriftungseinstellungen.BeschriftungssetTableModel.getRowCount(BeschriftungssetTableModel.java:36)
at javax.swing.table.DefaultTableModel.setDataVector(DefaultTableModel.java:224)
at javax.swing.table.DefaultTableModel.<init>(DefaultTableModel.java:124)
at javax.swing.table.DefaultTableModel.<init>(DefaultTableModel.java:106)
at javax.swing.table.DefaultTableModel.<init>(DefaultTableModel.java:86)
at ch.aaa.xxx.yyy.gruppen.plugin.anzeige.beschriftungseinstellungen.BeschriftungssetTableModel.<init>(BeschriftungssetTableModel.java:18)
at ch.aaa.xxx.yyy.gruppen.plugin.anzeige.beschriftungseinstellungen.BeschriftungssetTableModel.main(BeschriftungssetTableModel.java:50)
This question already has an answer here:
What's wrong with overridable method calls in constructors?
7 answers
java swing
java swing
edited Aug 17 at 7:21
asked Aug 17 at 7:13
user489872
1,28531335
1,28531335
marked as duplicate by Eugene
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Aug 17 at 19:24
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Eugene
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Aug 17 at 19:24
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
3
Could you add the stack trace for theNullPointerException
?
â deHaar
Aug 17 at 7:14
5
If your plan is to replace everything DefaultTableModel does by your own methods, and to use your own list rather than the data that DefaultTableModel uses, implement AbstractTableModel instead of extending DefaultTableModel.
â JB Nizet
Aug 17 at 7:29
3
Unrelated: consider not mixing languages in your code. And as BeschriftungsMengeBohne sounds really bad, I would rather go all EnglishInscriptionSetBean
for example.
â GhostCat
Aug 17 at 7:41
1
I would definately go with @JBNizet 's advice and extend AbstractTableModel
â keuleJ
Aug 17 at 8:06
1
@GuntramBlohm well it's complicated enough to know all the special terms that exist in german... let alone finding the correct translation... there would be absolutely no benefits for our projects since speaking german is a requirement
â user489872
Aug 18 at 15:21
 |Â
show 9 more comments
3
Could you add the stack trace for theNullPointerException
?
â deHaar
Aug 17 at 7:14
5
If your plan is to replace everything DefaultTableModel does by your own methods, and to use your own list rather than the data that DefaultTableModel uses, implement AbstractTableModel instead of extending DefaultTableModel.
â JB Nizet
Aug 17 at 7:29
3
Unrelated: consider not mixing languages in your code. And as BeschriftungsMengeBohne sounds really bad, I would rather go all EnglishInscriptionSetBean
for example.
â GhostCat
Aug 17 at 7:41
1
I would definately go with @JBNizet 's advice and extend AbstractTableModel
â keuleJ
Aug 17 at 8:06
1
@GuntramBlohm well it's complicated enough to know all the special terms that exist in german... let alone finding the correct translation... there would be absolutely no benefits for our projects since speaking german is a requirement
â user489872
Aug 18 at 15:21
3
3
Could you add the stack trace for the
NullPointerException
?â deHaar
Aug 17 at 7:14
Could you add the stack trace for the
NullPointerException
?â deHaar
Aug 17 at 7:14
5
5
If your plan is to replace everything DefaultTableModel does by your own methods, and to use your own list rather than the data that DefaultTableModel uses, implement AbstractTableModel instead of extending DefaultTableModel.
â JB Nizet
Aug 17 at 7:29
If your plan is to replace everything DefaultTableModel does by your own methods, and to use your own list rather than the data that DefaultTableModel uses, implement AbstractTableModel instead of extending DefaultTableModel.
â JB Nizet
Aug 17 at 7:29
3
3
Unrelated: consider not mixing languages in your code. And as BeschriftungsMengeBohne sounds really bad, I would rather go all English
InscriptionSetBean
for example.â GhostCat
Aug 17 at 7:41
Unrelated: consider not mixing languages in your code. And as BeschriftungsMengeBohne sounds really bad, I would rather go all English
InscriptionSetBean
for example.â GhostCat
Aug 17 at 7:41
1
1
I would definately go with @JBNizet 's advice and extend AbstractTableModel
â keuleJ
Aug 17 at 8:06
I would definately go with @JBNizet 's advice and extend AbstractTableModel
â keuleJ
Aug 17 at 8:06
1
1
@GuntramBlohm well it's complicated enough to know all the special terms that exist in german... let alone finding the correct translation... there would be absolutely no benefits for our projects since speaking german is a requirement
â user489872
Aug 18 at 15:21
@GuntramBlohm well it's complicated enough to know all the special terms that exist in german... let alone finding the correct translation... there would be absolutely no benefits for our projects since speaking german is a requirement
â user489872
Aug 18 at 15:21
 |Â
show 9 more comments
2 Answers
2
active
oldest
votes
up vote
28
down vote
accepted
The constructor of DefaultTableModel
calls getRowCount
before the subclass had a chance to initialize its content, thus leading to NPE with your implem. This is a bad design of the base class, as calling overridable methods from within a constructor is considered a bad practice, but hey, Swing API has quite a few of these :)
Cf. What's wrong with overridable method calls in constructors?
1
"...as calling overridable methods from within a constructor is considered a bad practice..." For precisely this reason. :-)
â T.J. Crowder
Aug 17 at 16:48
add a comment |Â
up vote
13
down vote
Do not extend DefaultTableModel
, extend AbstractTableModel
instead.
DefaultTableModel
uses a Vector to maintain the data and does not expect you having your own data in a subclass. As @spi wrote, it is calling getRowCount
which was overriden to return the size of data
that is not yet initialized.
The AbstractTableModel
is easy to extend (DefaultTableModel
also does), just these 3 methods need to be implemented (already done in questions code):
public int getRowCount();
public int getColumnCount();
public Object getValueAt(int row, int column);
Others methods can be overridden to adjust its behavior like isCellEditable
or getColumnClass
. If the data is changed the appropriate fireXXX
method should be called so the registered listeners are invoked (e.g. JTable
using that model).
An alternative is to implement TableModel
, but AbstractTableModel
takes care of some standard (nasty?) functionality like maintaining the listeners.
(bad alternative, just check for null
, but this probably will mess up with the DefaultTableModel
code - do not do this)
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
28
down vote
accepted
The constructor of DefaultTableModel
calls getRowCount
before the subclass had a chance to initialize its content, thus leading to NPE with your implem. This is a bad design of the base class, as calling overridable methods from within a constructor is considered a bad practice, but hey, Swing API has quite a few of these :)
Cf. What's wrong with overridable method calls in constructors?
1
"...as calling overridable methods from within a constructor is considered a bad practice..." For precisely this reason. :-)
â T.J. Crowder
Aug 17 at 16:48
add a comment |Â
up vote
28
down vote
accepted
The constructor of DefaultTableModel
calls getRowCount
before the subclass had a chance to initialize its content, thus leading to NPE with your implem. This is a bad design of the base class, as calling overridable methods from within a constructor is considered a bad practice, but hey, Swing API has quite a few of these :)
Cf. What's wrong with overridable method calls in constructors?
1
"...as calling overridable methods from within a constructor is considered a bad practice..." For precisely this reason. :-)
â T.J. Crowder
Aug 17 at 16:48
add a comment |Â
up vote
28
down vote
accepted
up vote
28
down vote
accepted
The constructor of DefaultTableModel
calls getRowCount
before the subclass had a chance to initialize its content, thus leading to NPE with your implem. This is a bad design of the base class, as calling overridable methods from within a constructor is considered a bad practice, but hey, Swing API has quite a few of these :)
Cf. What's wrong with overridable method calls in constructors?
The constructor of DefaultTableModel
calls getRowCount
before the subclass had a chance to initialize its content, thus leading to NPE with your implem. This is a bad design of the base class, as calling overridable methods from within a constructor is considered a bad practice, but hey, Swing API has quite a few of these :)
Cf. What's wrong with overridable method calls in constructors?
answered Aug 17 at 7:27
spi
721516
721516
1
"...as calling overridable methods from within a constructor is considered a bad practice..." For precisely this reason. :-)
â T.J. Crowder
Aug 17 at 16:48
add a comment |Â
1
"...as calling overridable methods from within a constructor is considered a bad practice..." For precisely this reason. :-)
â T.J. Crowder
Aug 17 at 16:48
1
1
"...as calling overridable methods from within a constructor is considered a bad practice..." For precisely this reason. :-)
â T.J. Crowder
Aug 17 at 16:48
"...as calling overridable methods from within a constructor is considered a bad practice..." For precisely this reason. :-)
â T.J. Crowder
Aug 17 at 16:48
add a comment |Â
up vote
13
down vote
Do not extend DefaultTableModel
, extend AbstractTableModel
instead.
DefaultTableModel
uses a Vector to maintain the data and does not expect you having your own data in a subclass. As @spi wrote, it is calling getRowCount
which was overriden to return the size of data
that is not yet initialized.
The AbstractTableModel
is easy to extend (DefaultTableModel
also does), just these 3 methods need to be implemented (already done in questions code):
public int getRowCount();
public int getColumnCount();
public Object getValueAt(int row, int column);
Others methods can be overridden to adjust its behavior like isCellEditable
or getColumnClass
. If the data is changed the appropriate fireXXX
method should be called so the registered listeners are invoked (e.g. JTable
using that model).
An alternative is to implement TableModel
, but AbstractTableModel
takes care of some standard (nasty?) functionality like maintaining the listeners.
(bad alternative, just check for null
, but this probably will mess up with the DefaultTableModel
code - do not do this)
add a comment |Â
up vote
13
down vote
Do not extend DefaultTableModel
, extend AbstractTableModel
instead.
DefaultTableModel
uses a Vector to maintain the data and does not expect you having your own data in a subclass. As @spi wrote, it is calling getRowCount
which was overriden to return the size of data
that is not yet initialized.
The AbstractTableModel
is easy to extend (DefaultTableModel
also does), just these 3 methods need to be implemented (already done in questions code):
public int getRowCount();
public int getColumnCount();
public Object getValueAt(int row, int column);
Others methods can be overridden to adjust its behavior like isCellEditable
or getColumnClass
. If the data is changed the appropriate fireXXX
method should be called so the registered listeners are invoked (e.g. JTable
using that model).
An alternative is to implement TableModel
, but AbstractTableModel
takes care of some standard (nasty?) functionality like maintaining the listeners.
(bad alternative, just check for null
, but this probably will mess up with the DefaultTableModel
code - do not do this)
add a comment |Â
up vote
13
down vote
up vote
13
down vote
Do not extend DefaultTableModel
, extend AbstractTableModel
instead.
DefaultTableModel
uses a Vector to maintain the data and does not expect you having your own data in a subclass. As @spi wrote, it is calling getRowCount
which was overriden to return the size of data
that is not yet initialized.
The AbstractTableModel
is easy to extend (DefaultTableModel
also does), just these 3 methods need to be implemented (already done in questions code):
public int getRowCount();
public int getColumnCount();
public Object getValueAt(int row, int column);
Others methods can be overridden to adjust its behavior like isCellEditable
or getColumnClass
. If the data is changed the appropriate fireXXX
method should be called so the registered listeners are invoked (e.g. JTable
using that model).
An alternative is to implement TableModel
, but AbstractTableModel
takes care of some standard (nasty?) functionality like maintaining the listeners.
(bad alternative, just check for null
, but this probably will mess up with the DefaultTableModel
code - do not do this)
Do not extend DefaultTableModel
, extend AbstractTableModel
instead.
DefaultTableModel
uses a Vector to maintain the data and does not expect you having your own data in a subclass. As @spi wrote, it is calling getRowCount
which was overriden to return the size of data
that is not yet initialized.
The AbstractTableModel
is easy to extend (DefaultTableModel
also does), just these 3 methods need to be implemented (already done in questions code):
public int getRowCount();
public int getColumnCount();
public Object getValueAt(int row, int column);
Others methods can be overridden to adjust its behavior like isCellEditable
or getColumnClass
. If the data is changed the appropriate fireXXX
method should be called so the registered listeners are invoked (e.g. JTable
using that model).
An alternative is to implement TableModel
, but AbstractTableModel
takes care of some standard (nasty?) functionality like maintaining the listeners.
(bad alternative, just check for null
, but this probably will mess up with the DefaultTableModel
code - do not do this)
edited Aug 17 at 7:49
answered Aug 17 at 7:38
Carlos Heuberger
23.4k85077
23.4k85077
add a comment |Â
add a comment |Â
3
Could you add the stack trace for the
NullPointerException
?â deHaar
Aug 17 at 7:14
5
If your plan is to replace everything DefaultTableModel does by your own methods, and to use your own list rather than the data that DefaultTableModel uses, implement AbstractTableModel instead of extending DefaultTableModel.
â JB Nizet
Aug 17 at 7:29
3
Unrelated: consider not mixing languages in your code. And as BeschriftungsMengeBohne sounds really bad, I would rather go all English
InscriptionSetBean
for example.â GhostCat
Aug 17 at 7:41
1
I would definately go with @JBNizet 's advice and extend AbstractTableModel
â keuleJ
Aug 17 at 8:06
1
@GuntramBlohm well it's complicated enough to know all the special terms that exist in german... let alone finding the correct translation... there would be absolutely no benefits for our projects since speaking german is a requirement
â user489872
Aug 18 at 15:21