gpt4 book ai didi

java - AbstractTableModel 中的空指针

转载 作者:行者123 更新时间:2023-12-02 06:35:18 26 4
gpt4 key购买 nike

我正在尝试用 VCID 和 VCIDBACKUP 的空值替换“Dont Have”。这是我的代码:

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();

这是我的 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];
}

}

当我调试 setValueAt 方法时,我得到了正确的值,但仍然遇到相同的错误。我可能忘记了 AbstractModel 类中的一些实现,我不太清楚。有人可以帮忙吗?

最佳答案

下面两行

public void setValueAt(Object aValue, int rowIndex, int columnIndex){
Macro macro = new Macro();

应替换为

public void setValueAt(Object aValue, int rowIndex, int columnIndex){
Macro macro = linhas.get(rowIndex);

否则,您将修改一个甚至不属于模型一部分的新宏,并且该新宏在 setValueAt() 方法返回后立即有资格进行 GC。您想要更改模型中此行索引处的宏的值。

也就是说,我发现在将模型导出到 Excel 时修改模型有点奇怪。为什么模型不自己进行替换:

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;
}

关于java - AbstractTableModel 中的空指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19731695/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com