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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30534224/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 17:17:18  来源:igfitidea点击:

Null Pointer Exception while reading from excel?

javaselenium-webdrivernullpointerexceptionapache-poi

提问by Ron

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


getting null pointer exception when first cell is empty. I have searched many blogs but couldn't find any solution can anyone please help me with code.

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

回答by Naman Gala

Make sure you add a null check before using toString()method otherwise you will get NPEif the value is null.

确保在使用toString()方法之前添加空检查,否则NPE如果值为null.

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

Refer this SO question

参考这个 SO question

回答by Uma Kanth

for every .toString()add a " "+ x.toString()to it, so that the value doesnt become a null. `

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

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


`

回答by KiranVegi

try below Code its working fine for me, we cannot define length of a Null

试试下面的代码,它对我来说工作正常,我们无法定义 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");
            }
        }