gpt4 book ai didi

java - XSSF。空指针异常

转载 作者:行者123 更新时间:2023-12-01 17:21:17 27 4
gpt4 key购买 nike

private void cleaner(Integer columnsCount, Integer rowsCount, Object object){
int firstColumn=0;
int firstRow=0;
XSSFSheet sheet = workBook.getSheetAt(0);
for (int lineId=firstRow;lineId<rowsCount;lineId++) {
XSSFRow row = sheet.getRow(lineId);
for (int columnId=firstColumn;columnId<columnsCount;columnId++){
row.createCell(columnId).setCellValue(object.toString()); }
}
}

我真的不明白这个逻辑。首先,我得到行,然后尝试写入数据。我当然知道,我的所有数据都不为空。但编译器说:

Exception in thread "main" java.lang.NullPointerException
at workhere.WriterXlsx.cleaner(WriterXlsx.java:75)
at workhere.WriterXlsx.<init>(WriterXlsx.java:19)
at workhere.Start.main(Start.java:28)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

WriterXlsx.java:75 : row.createCell(columnId).setCellValue(object.toString());

我用以下代码开始此方法:cleaner(30,30,"");

最佳答案

看看API

sheet.getRow(lineId);

如果行不存在则返回 null。您需要create在访问该行之前先添加一行,以防该行不存在(或跳过它)。

关于java - XSSF。空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18586564/

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