簡體   English   中英

讀取表格Excel文件時在硒中獲取空指針異常,無法找到解決方法

[英]Getting null pointer exception in selenium when reading form Excel File, unable to find work around

我是selenium的新手,我編寫了一個關鍵字驅動的框架,該框架從excel文件讀取並根據當前值執行操作。 因此,在讀取文件時,在特定行中,如果第一個單元格包含值,則為測試用例名稱,否則為關鍵字。

現在,當它讀取沒有任何值的第一個單元格時,它將為該行引發java.lang.NullPointerException。

以下是我的代碼的一部分:

   Sheet sheet = file.ReadExcelFile("E:\\workspace\\Practice1", "Keywords.xlsx", "Sheet1");
    int rowCount = sheet.getLastRowNum()-sheet.getFirstRowNum();
     for (int i = 1; i < rowCount+1; i++) {    
        Row row = sheet.getRow(i);
        //Below line gives me null pointer error
        if(row.getCell(0).toString().length()==0){  
            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");
            }
    }

我試圖使用三元(?)運算符來解析,如下所示:if(row.getCell(0)== null?“”:row.getCell(0))

但由於無法從對象轉換為布爾值而導致錯誤; 嘗試將其類型化為布爾值,但這也不起作用。

還有其他更好的書寫方式嗎

您正在提議更換生產線

if(row.getCell(0).toString().length()==0){  

與線

if(row.getCell(0)==null?"":row.getCell(0)) {

但這將不起作用,因為表達式row.getCell(0)==null?"":row.getCell(0)沒有boolean值。 它具有值""Stringrow.getCell(0) ,我認為它返回Object

如果row.getCell(0)為空字符串或null ,則似乎要運行if塊的內容。 如果是這種情況,那么最簡單的方法就是編寫

if(row.getCell(0) == null || row.getCell(0).toString().length()==0){  

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM