繁体   English   中英

从Excel读取时出现空指针异常?

[英]Null Pointer Exception while reading from excel?

public class ExecuteTest {
@Test

public void testLogin() throws Exception {
    // TODO Auto-generated method stub
`WebDriver webdriver = new FirefoxDriver();

ReadExcelFile file = new ReadExcelFile();
ReadObject object = new ReadObject();
Properties allObjects = object.getObjectRepository();
UIOperation operation = new UIOperation(webdriver);
//Read keyword sheet
Sheet RDSheet = file.readExcel(System.getProperty("user.dir")+"\\","TestCase.xlsx" , "KeywordFramework");
//Find number of rows in excel file
int rowCount =        //Loop over all the rows RDSheet.getLastRowNum()-RDSheet.getFirstRowNum();
    //Create a loop over all the rows of excel file to read it
    for (int i = 1; i < rowCount+1; i++) {
        Row row = RDSheet.getRow(i);
        //Check if the first cell contain a value, if yes, That means it is the new testcase name
    if(row.getCell(0).toString().length()==0){
        //Print testcase detail on console
        System.out.println(row.getCell(1).toString()+"----"+ row.getCell(2).toString()+"----"+
        row.getCell(3).toString()+"----"+ row.getCell(4).toString());
        //Call perform function to perform operation on UI
        operation.perform(allObjects, row.getCell(1).toString(), row.getCell(2).toString(),
                row.getCell(3).toString(), row.getCell(4).toString());
     }
else{

//Print the new testcase name when it started
            System.out.println("New Testcase->"+row.getCell(0).toString() +" Started");
            }
}
}
}

当第一个单元格为空时,获取空指针异常。 我搜索了许多博客,但找不到任何解决方案,有人可以帮助我提供代码。

确保在使用toString()方法之前添加了一个null检查,否则,如果该值为null则将获得NPE

if (row.getCell(0) != null) {
     row.getCell(0).toString() //in this case you are trying to call toString on null value.
}

请参阅此SO question

为每个.toString()添加一个" "+ x.toString() ,以便该值不为null。 `

for (int i = 1; i < rowCount+1; i++) {
        Row row = RDSheet.getRow(i);
        //Check if the first cell contain a value, if yes, That means it is the new testcase name
    if((row.getCell(0)+"").toString().length()==0){
        //Print testcase detail on console
        System.out.println((row.getCell(1)+"").toString()+"----"+ (row.getCell(2)+"").toString()+"----"+
        (row.getCell(3)+"").toString()+"----"+ (row.getCell(4)+"").toString());
        //Call perform function to perform operation on UI
        //your operations
     }
else{

//Print the new testcase name when it started
            System.out.println("New Testcase->"+row.getCell(0).toString() +" Started");
            }
}


`

尝试下面的代码为我工作正常,我们不能定义一个Null的长度

if(row.getCell(0)==null){
            //Print testcase detail on console
//System.out.println("testing");
            System.out.println(row.getCell(1)+"----"+ row.getCell(2)+"----"+
                row.getCell(3)+"----"+ row.getCell(4));
            //Call perform function to perform operation on UI
            try{
                operation.perform(allObjects, row.getCell(1)==null?"Not a Valid KeyWord":row.getCell(1).toString(), 
                        row.getCell(2)==null?null:row.getCell(2).toString(),
                                row.getCell(3)==null?null:row.getCell(3).toString(), 
                                        row.getCell(4)==null?null:row.getCell(4).toString());
            }catch(Exception e)
            {
                //will print Error Row and break the flow
                System.out.println(i);
                System.out.println(e);
                break;
            }
     }
        else{
            //Print the new testcase name when it started
                System.out.println("New Testcase->"+row.getCell(0) +" Started");
            }
        }

暂无
暂无

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

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