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.

6
  • 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? Commented May 29, 2015 at 16:11
  • please add your stack trace. Commented May 29, 2015 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 Commented May 29, 2015 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 Commented May 29, 2015 at 16:14
  • String x = row.getCell(0); if(x != null) {//do stuff} x = row.getCell(1); if(x! = null){//do your stuff} Commented May 29, 2015 at 16:19

3 Answers 3

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


`
Sign up to request clarification or add additional context in comments.

2 Comments

Refering this answer, 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.
This is not the case. Even I had the same problem, I did the same and it worked for me. @Naman Gala
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

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.