Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Netbeans-Java Excel NullPointerException_Java_Excel - Fatal编程技术网

Netbeans-Java Excel NullPointerException

Netbeans-Java Excel NullPointerException,java,excel,Java,Excel,我在excel文件中写东西有问题。我正在使用ApachePOI在excel上编写 我的代码是: private void EscreverExcel(String Nome) throws FileNotFoundException, IOException{ File src = new File("D:\\Work\\Fortune\\DadosCliente.xlsx"); FileInputStream fist = new FileInputStream(src);

我在excel文件中写东西有问题。我正在使用ApachePOI在excel上编写

我的代码是:

private void EscreverExcel(String Nome) throws FileNotFoundException, IOException{
    File src = new File("D:\\Work\\Fortune\\DadosCliente.xlsx");

    FileInputStream fist = new FileInputStream(src);
    XSSFWorkbook wb = new XSSFWorkbook(fist);
    XSSFSheet sheet = wb.getSheetAt(0);

    sheet.getRow(0).createCell(2).setCellValue(Nome);
    FileOutputStream fos = new FileOutputStream(src);
    wb.write(fos);
    wb.close();
}
public static void main(String[] args) throws IOException{
   Excel ex = new Excel();
   ex.EscreverExcel("Mário");
}
当我运行程序时,他们会告诉我:

Exception in thread "main" java.lang.NullPointerException

at fortunewheel.Excel.EscreverExcel(Excel.java:79)
at fortunewheel.Excel.main(Excel.java:87)

我做错了什么?你能帮我一下吗?

好的,请检查表格。getRow(0)不等于null。

好的,请检查表格。getRow(0)不等于null。

既然你说了,第79行就是下一行

sheet.getRow(0).createCell(2).setCellValue(Nome);
我的猜测是,
sheet
是一个null对象,或者,
sheet.getRow()
返回null。我建议您同时检查它们,以避免出现空指针异常

根据以下官方文件:

getRow()
方法返回逻辑行(基于0)。如果您请求未定义的行,则会得到null。也就是说,第4行表示图纸上的第五行

参数:

rownum - row to get
返回:

XSSFRow representing the rownumber or null if its not defined on the sheet

所以我猜,
getRow()
在您的情况下返回null。

既然您说过,第79行就是下面的一行

sheet.getRow(0).createCell(2).setCellValue(Nome);
我的猜测是,
sheet
是一个null对象,或者,
sheet.getRow()
返回null。我建议您同时检查它们,以避免出现空指针异常

根据以下官方文件:

getRow()
方法返回逻辑行(基于0)。如果您请求未定义的行,则会得到null。也就是说,第4行表示图纸上的第五行

参数:

rownum - row to get
返回:

XSSFRow representing the rownumber or null if its not defined on the sheet

所以我猜,
getRow()
在您的情况下返回null。

哪一行可能是第79行的重复?第79行是:“sheet.getRow(0).createCell(2).setCellValue(Nome);”第87行是:“ex.EscreverExcel(“Mário”);“哪一行可能是第79行的重复?第79行是:“sheet.getRow(0).createCell(2).setCellValue(Nome)第87行是:“ex.EscreverExcel(“Mário”);”工作表。getRow(0)为Null,这就是为什么您有NPESheet。getRow(0)为Null,这就是为什么您有NPEgetRow(0)返回Null谢谢您的帮助^getRow(0)返回Null谢谢您的帮助^^