繁体   English   中英

从Excel返回单元格值时出现空指针异常

[英]Null pointer exception while returning cell value from excel

我有以下代码

private void execute_TestCase()引发异常{

    int iTotalTestCases = ExcelUtils.getRowCount(Constants.Sheet_TestCases);
    System.out.println("Total TC count" + iTotalTestCases);
    **formname=ExcelUtils.getCellData(13, 6, Constants.Sheet_TestCases);**
    System.out.println("F"+formname);

    for(int iTestcase=1;iTestcase<iTotalTestCases;iTestcase++){
        bResult = true;
        sTestCaseID = ExcelUtils.getCellData(iTestcase, Constants.Col_TestCaseID, Constants.Sheet_TestCases); 
        sRunMode = ExcelUtils.getCellData(iTestcase, Constants.Col_RunMode,Constants.Sheet_TestCases);
        if (sRunMode.equals("Yes")){
            Log.startTestCase(sTestCaseID);
            iTestStep = ExcelUtils.getRowContains(sTestCaseID, Constants.Col_TestCaseID, Constants.Sheet_TestSteps);
            iTestLastStep = ExcelUtils.getTestStepsCount(Constants.Sheet_TestSteps, sTestCaseID, iTestStep);
            bResult=true;
            System.out.println("sTestCaseID"+ sTestCaseID);
            System.out.println("iTestStep"+ iTestStep);
            System.out.println("iTestLastStep"+ iTestLastStep);
            for (;iTestStep<iTestLastStep;iTestStep++){
                sActionKeyword = ExcelUtils.getCellData(iTestStep, Constants.Col_ActionKeyword,Constants.Sheet_TestSteps);
                sPageObject = ExcelUtils.getCellData(iTestStep, Constants.Col_PageObject, Constants.Sheet_TestSteps);
                sData = ExcelUtils.getCellData(iTestStep, Constants.Col_DataSet, Constants.Sheet_TestSteps);
                execute_Actions();
                if(bResult==false){
                    ExcelUtils.setCellData(Constants.KEYWORD_FAIL,iTestcase,Constants.Col_Result,Constants.Sheet_TestCases);
                    Log.endTestCase(sTestCaseID);
                    break;
                    }                       
                }
            if(bResult==true){
            ExcelUtils.setCellData(Constants.KEYWORD_PASS,iTestcase,Constants.Col_Result,Constants.Sheet_TestCases);
            Log.endTestCase(sTestCaseID);   
                }                   
            }
        }
    }   

formname = ExcelUtils.getCellData(13,6,Constants.Sheet_TestCases)。 在getcelldata中调用NullPointerException时。 但是如果使用相同的(13,6)对,则其底部效果很好。 在下面找到getCellData代码

 public static String getCellData(int RowNum, int ColNum, String SheetName ) throws Exception{
                try{
                    System.out.println("sheet"+SheetName);
                    ExcelWSheet = ExcelWBook.getSheet(SheetName);
                    System.out.println("Excelwsheet"+ExcelWSheet);
                    Cell = ExcelWSheet.getRow(RowNum).getCell(ColNum);
                    System.out.println("test here2");
                    String CellData = Cell.getStringCellValue();
                    System.out.println("Celldata"+CellData);
                    return CellData;
                 }catch (Exception e){
                     System.out.println("Exception" + e);
                     Log.error("Class Utils | Method getCellData | Exception desc : "+e.getMessage());
                     DriverScript.bResult = true;
                     return"";
                     }
                 }

Sheet.getRow(int)Apache POI javadocs

返回-表示行号的行;如果未在工作表中定义,则返回null

因此,您需要更改此行而不进行null检查:

Cell = ExcelWSheet.getRow(RowNum).getCell(ColNum);

对于类似这样的东西,它会检查+处理该行(或单元格!)是否为空:

 Row row = ExcelWSheet.getRow(RowNum);
 if (row == null) {
    // Handle empty row
    return "";
 }
 Cell cell = row.getCell(ColNum, Row.RETURN_BLANK_AS_NULL);
 if (cell == null) {
    // Handle empty cell
    return "";
 }

您可能还想处理包含除字符串以外的内容的单元格,以及其他一些常见问题...阅读Apache POI Spreadsheet文档,以了解您需要做的事情!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM