java - 尝试将结果写入 Excel 时出现 NullPointerException

标签 java excel selenium-webdriver

有人可以帮忙处理这段代码吗?
我在第 41 行收到有关 NullPointerException 的错误消息。我知道它返回 null,但以下验证需要该值。一般任务是将结果写入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();
                }

        }


    }

最佳答案

将结果字符串从测试方法传递到方法 writeexcel。您不需要创建对象,因为该方法是静态的

writeexcel("pass",2,3);

这将调用 writeexcel 方法,并将作为参数传递的字符串写入相应的单元格中,即“pass”

方法:

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

    }
    }

希望这对您有帮助...如果您需要任何进一步的帮助,请回来

关于java - 尝试将结果写入 Excel 时出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31783908/

相关文章:

java - 如何使用POI从java中基于某些唯一值的excel读取特定行?

java - 如何在 Selenium 中使用 CSS 选择器找到 TinyMCE 编辑器的主体?

Java cipherinputstream 将所有输入数据变为0

java - 为什么这两种算法的运行时间存在差异?

excel - 比较两列并导出唯一值

javascript - 如何通过 Selenium webdriver 处理 Abhibus 回程的第二个日期选择器?

python - 不使用 WebDriverWait 我的代码返回 : element click intercepted/with WebDriverWait returns 'NoneType' object is not iterable

java - 我不知道如何通过windows命令提示符输入

java - 用正则表达式替换第一个

c# - 查找左上角单元格的坐标?使用 EPPlus