尝试将结果写入excel时发生NullPointerException

有人可以帮助这个代码?
我收到有关NullPointerException在41行的错误消息。 我知道它返回null,但是这个值是以下validation所必需的。 一般的任务是将结果写入excel文件。

public class ExcelUtils { public static void main(String[] args) { //Blank workbook XSSFWorkbook workbook = new XSSFWorkbook(); //Create a blank sheet XSSFSheet sheet = workbook.createSheet("Employee Data"); String Result = "TEST TEST TEST"; int RowNum = 2; int ColNum = 3; XSSFSheet ExcelWSheet = sheet; XSSFCell Cell; XSSFRow Row; try { Row = ExcelWSheet.getRow(RowNum); Cell = Row.getCell(ColNum, org.apache.poi.ss.usermodel.Row.RETURN_BLANK_AS_NULL); if (Cell == null) { Cell = Row.createCell(ColNum); Cell.setCellValue(Result); } else { Cell.setCellValue(Result); } //--------------------------------------------------- //try // { //Write the workbook in file system FileOutputStream out = new FileOutputStream(new File("howtodoinjava_demo.xlsx")); workbook.write(out); out.close(); System.out.println("howtodoinjava_demo.xlsx written successfully on disk."); } catch (Exception e) { e.printStackTrace(); } } } 

将Resultstring从testing方法传递给writeexcel方法。由于方法是静态的,因此不需要创build对象

 writeexcel("pass",2,3); 

这将调用writeexcel方法,并将写入作为parameter passing的string即“通过”在相应的单元格中

方法

 public static void writeexcel(String Result,int RowNum ,int ColNum) { //Blank workbook XSSFWorkbook workbook = new XSSFWorkbook(); //Create a blank sheet XSSFSheet sheet = workbook.createSheet("Employee Data"); XSSFSheet ExcelWSheet = sheet; XSSFCell Cell; XSSFRow Row; try { Row = ExcelWSheet.createRow(RowNum); Cell = Row.getCell(ColNum, org.apache.poi.ss.usermodel.Row.RETURN_BLANK_AS_NULL); if (Cell == null) { Cell = Row.createCell(ColNum); Cell.setCellValue(Result); } else { Cell.setCellValue(Result); } FileOutputStream out = new FileOutputStream(new File("howtodoinjava_demo.xlsx")); workbook.write(out); out.close(); System.out.println("howtodoinjava_demo.xlsx written successfully on disk."); } catch (Exception e) { e.printStackTrace(); } } 

编辑

 public class Tests { static XSSFWorkbook workbook = new XSSFWorkbook(); static XSSFSheet sheet = workbook.createSheet("Employee Data"); public static void main(String[] args) { int j=3; for(int i=2;i<=5;i++) { String result = "Test "+i; writeexcel(result, i, j); } writetoexcel();//write to cell(2,3),(3,3),(4,3) } public static void writeexcel(String Result,int RowNum ,int ColNum) { //Create a blank sheet XSSFSheet ExcelWSheet = sheet; XSSFCell Cell; XSSFRow Row; try { Row = ExcelWSheet.createRow(RowNum); Cell = Row.getCell(ColNum, org.apache.poi.ss.usermodel.Row.RETURN_BLANK_AS_NULL); if (Cell == null) { Cell = Row.createCell(ColNum); Cell.setCellValue(Result); } else { Cell.setCellValue(Result); } } catch (Exception e) { e.printStackTrace(); } } public static void writetoexcel(){ try{ FileOutputStream out = new FileOutputStream(new File("howtodoinjava_demo.xlsx")); workbook.write(out); out.close(); System.out.println("howtodoinjava_demo.xlsx written successfully on disk."); }catch (Exception e) { e.printStackTrace(); } } } 

希望这可以帮助你…请回来,如果你需要任何进一步的帮助

 static String sheetname = "TEST"; static XSSFWorkbook workbook = new XSSFWorkbook(); static XSSFSheet sheet = workbook.createSheet(sheetname); public static void writeexcel(String Result,int RowNum ,int ColNum) { //Create a blank sheet XSSFSheet ExcelWSheet = sheet; XSSFCell Cell; XSSFRow Row; try { Row = ExcelWSheet.createRow(RowNum); Cell = Row.getCell(ColNum, org.apache.poi.ss.usermodel.Row.RETURN_BLANK_AS_NULL); if (Cell == null) { Cell = Row.createCell(ColNum); Cell.setCellValue(Result); } else { Cell.setCellValue(Result); } } catch (Exception e) { e.printStackTrace(); } } public static void writetoexcel(String namefile) throws IOException{ String namefile1 = namefile+".xlsx"; if(namefile1.equals(namefile1)){ try { // Open the Excel file FileInputStream ExcelFile = new FileInputStream(namefile1); // Access the required test data sheet workbook = new XSSFWorkbook(ExcelFile); sheet = workbook.getSheet(sheetname); System.out.println("This file is already exist. The system is opened this file for adding information"); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } else { try{ FileOutputStream out = new FileOutputStream(new File(namefile1)); workbook.write(out); out.close(); System.out.println("howtodoinjava_demo.xlsx written successfully on disk."); }catch (Exception e) { e.printStackTrace(); } } } }