尝试将结果写入excel时出现NullPointerException

时间:2015-08-03 09:31:28

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

        }


    }

2 个答案:

答案 0 :(得分:1)

将测试方法中的Result字符串传递给方法writeexcel。由于方法是静态的,所以不需要创建对象

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

    }

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

方法

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

    }
    }

修改

angularjs

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

答案 1 :(得分:0)

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

}


}