[英]NullPointer in AbstractTableModel
I am trying to substitute the null value of VCID and VCIDBACKUP for "Dont Have". 我试图将VCID和VCIDBACKUP的空值替换为“Dont Have”。 Here is my code:
这是我的代码:
if (controladorExcel == false) {
WritableWorkbook workbookVazio = Workbook.createWorkbook(file);
WritableSheet sheet1 = workbookVazio.createSheet("First Sheet", 0);
TableModel model = table.getModel();
for (int i = 0; i < model.getColumnCount(); i++) {
Label column = new Label(i, 0, model.getColumnName(i));
sheet1.addCell(column);
System.out.println(column.getContents());
}
int j = 0;
for (int i = 0; i < model.getRowCount(); i++) {
for (j = 0; j < model.getColumnCount(); j++) {
System.out.println(model.getRowCount());
System.out.println(model.getColumnCount());
if(model.getValueAt(i, j) == null){ //At this point I verify if the value is null
model.setValueAt("Nao possui", i, j);
}
Label row = new Label(j, i + 1, //I got NULL POINTER here
model.getValueAt(i, j).toString());
System.out.println(row.getContents());
sheet1.addCell(row);
}
}
workbookVazio.write();
workbookVazio.close();
Here is the code of my AbstractTableModel: 这是我的AbstractTableModel的代码:
public class MacroTableModel extends AbstractTableModel {
private String[] colunas;
private List<Macro> linhas;
public MacroTableModel(List<Macro> lista){
this.colunas = new String[]{"VPN Name", "VCID", "VCID BACKUP"};
this.linhas = new ArrayList<Macro>(lista);
}
public String getColumnName(int index) {
return colunas[index];
}
public int getRowCount(){
return linhas.size();
}
public int getColumnCount(){
return colunas.length;
}
@Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex){
Macro macro = new Macro();
switch(columnIndex){
case 0:
macro.setVpnName(aValue.toString());
break;
case 1:
macro.setVcid(aValue.toString());
break;
case 2:
macro.setVcid_BackUp(aValue.toString());
break;
}
fireTableCellUpdated(rowIndex,columnIndex);
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
Macro macro = linhas.get(rowIndex);
switch(columnIndex){
case 0:
return macro.getVpnName();
case 1:
return macro.getVcid();
case 2:
return macro.getVcid_BackUp();
}
return null;
}
public void setColunas(String[] colunas) {
this.colunas = colunas;
}
public String getColunas(int i) {
return colunas[i];
}
} }
When I debug the setValueAt method I get the correct value, but I still get the same error. 当我调试setValueAt方法时,我得到正确的值,但我仍然得到相同的错误。 I might forgot some implementation in my AbstractModel class, I dont know exactly.
我可能忘记了我的AbstractModel类中的一些实现,我完全不知道。 Can someone help, please ?
有人可以帮帮忙吗?
The following two lines 以下两行
public void setValueAt(Object aValue, int rowIndex, int columnIndex){
Macro macro = new Macro();
should be replaced by 应该被替换
public void setValueAt(Object aValue, int rowIndex, int columnIndex){
Macro macro = linhas.get(rowIndex);
Otherwise, you're modifying a new Macro that is not even part of the model, and this new Macro becomes eligible to GC right after the setValueAt() method returns. 否则,您正在修改一个甚至不属于模型的新宏,并且这个新的宏在setValueAt()方法返回后立即符合GC的条件。 You want to change the value of the Macro that is in the model, at this row index.
您希望在此行索引处更改模型中的宏的值。
That said, I find it a bit strange to modify the model when exporting it to Excel. 也就是说,我发现在将模型导出到Excel时修改模型有点奇怪。 Why doesn't the model do the substitution by itself:
为什么模型不能自行替换:
public Object getValueAt(int rowIndex, int columnIndex) {
Macro macro = linhas.get(rowIndex);
switch(columnIndex){
case 0:
return valueOrDontHave(macro.getVpnName());
break;
case 1:
return valueOrDontHave(macro.getVcid());
case 2:
return valueOrDontHave(macro.getVcid_BackUp());
}
return null;
}
private valueOrDontHave(Object value) {
return value == null ? ""Nao possui" : value;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.