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

получение исключения null указателя, когда первая ячейка пуста. Я искал много блогов, но не смог найти решение Может кто-нибудь, пожалуйста, помогите мне с кодом.Исключение Null Pointer при чтении от excel?

+2

Что вы понимаете о NullPointerException, когда вы читаете о них? Что за путаница? Можете ли вы прочитать трассировку стека? Где трассировка стека? Вы пытались отлаживать отладчик? – Kon

+0

добавьте трассировку стека. –

+1

, если ячейка пуста, и вы хотите, чтобы она возвращала значение null. добавьте условие '(variable! = null)', чтобы избежать NullPointerException –

ответ

1

для каждого .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"); 
      } 
} 


` 
+0

Ссылаясь на этот [' answer'] (http://stackoverflow.com/a/4260769/3898076), 'row.getCell (0) +" "' приведет к строке 'null'. Таким образом, значение длины будет '4' вместо' 0'. @Ron убедитесь, что вы отлаживаете свой код и проверяете погоду, он работает должным образом. –

+0

Это не тот случай. Даже у меня была такая же проблема, я сделал то же самое, и это сработало для меня. @Naman Gala –

+0

Thats nice .. Я проверю ваш код. –

1

Убедитесь, что вы добавили нулевую проверку перед использованием метода toString(), иначе вы получите NPE, если значение null.

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

См этот SO question

0

попытка ниже кодекса его работы хорошо для меня, мы не можем определить длину 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"); 
      } 
     } 
Смежные вопросы