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

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.

Ron
  • 43
  • 1
  • 1
  • 9
  • 2
    What did you understand about a NullPointerException when you read about them? What's the confusion? Can you read a stack trace? Where is the stack trace? Did you try debugging with a debugger? – Kon May 29 '15 at 16:11
  • please add your stack trace. – Naman Gala May 29 '15 at 16:11
  • 1
    if a cell is empty and you want the info it returns null. add a condition `( variable != null)` to avoid NullPointerException – Uma Kanth May 29 '15 at 16:12
  • @Uma Lakshmi Kanth row.getCell(0) is empty cell. I want to navigate to next cell i.e getCell(1) to print the values.can you please help me with code – Ron May 29 '15 at 16:14
  • `String x = row.getCell(0); if(x != null) {//do stuff} x = row.getCell(1); if(x! = null){//do your stuff}` – Uma Kanth May 29 '15 at 16:19
  • possible duplicate of http://stackoverflow.com/q/218384/3898076 – Naman Gala May 29 '15 at 16:32

3 Answers3

1

Make sure you add a null check before using toString() method otherwise you will get NPE if the value is 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

Community
  • 1
  • 1
Naman Gala
  • 4,480
  • 1
  • 18
  • 48
1

for every .toString() add a " "+ x.toString() to it, so that the value doesnt become a 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");
            }
}


`
Uma Kanth
  • 5,614
  • 2
  • 16
  • 40
  • Refering this [`answer`](http://stackoverflow.com/a/4260769/3898076), `row.getCell(0)+""` will result in string `null`. So the length value will be `4` instead of `0`. @Ron make sure you debug your code and check weather it is working as expected. – Naman Gala May 29 '15 at 16:42
  • This is not the case. Even I had the same problem, I did the same and it worked for me. @Naman Gala – Uma Kanth May 29 '15 at 17:01
0

try below Code its working fine for me, we cannot define length of a 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");
            }
        }