Test fails ExcelReader | Selenium Forum
M
Posted on 19/01/2016
[attachment=1:2yvgrfga]XlJAR11.zip[/attachment:2yvgrfga]Hi Ashish,

1> I have created a function to read data from xlfile.
2> I have created a JAR File of those class files.
3> When I used for loop to print data from XL , it writes values of only one row and gives NULL POINTER exception after that.

If i create a new java file with main then it works fine but doesnot work along with testng.

Can you please help on what i am doing wrong. I am attaching JAR file , XL file and code.

******************** Test File ***********************
public class Test_A1 {

@Test
public void A() throws IOException{

configxl xl= new configxl("F://SeleniumXLFiles//B.xls");
int s=xl.totalRowsInSheet(0);
System.out.println(s);
//int totalrows=xl.totalRowsInSheet(0);
for(int i=2;i<13;i++){

for(int j=0;j<13;j++){
System.out.println(xl.getdata(0, i, j));
}
}
}
}

******************** END Test File ***********************

****************Actual methods***********************
import java.io.FileInputStream;
import java.io.IOException;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
//import org.apache.poi.ss.usermodel.Cell;

public class configxl {


HSSFSheet sht;
HSSFWorkbook wb;
HSSFRow row;
HSSFCell cell;
String data="";
FileInputStream file;
int totalrows;
public configxl(String path) throws IOException


{
file = new FileInputStream(path);

wb = new HSSFWorkbook(file);

// sht = wb.getSheetAt(0);

}

/*public int getrowcount(int sheetn)
{
//totalrows=0;
wb.getSheetAt(sheetn);
totalrows= sht.getLastRowNum();
return totalrows;

}*/



public String getdata(int sheetn,int row,int col)
{
sht = wb.getSheetAt(sheetn);
cell= sht.getRow(row).getCell(col);
if(cell.getCellType()==cell.CELL_TYPE_NUMERIC)

{
String celltext=String.valueOf(cell.getNumericCellValue());
return celltext;
}

else{
if(cell.getCellType()==cell.CELL_TYPE_STRING)
data = cell.getStringCellValue();
return data;
}


}




public double NoofColumns(int sheetn,int rnum)
{

wb.getSheetAt(sheetn);
int lastcellnum= sht.getRow(rnum).getLastCellNum();
return lastcellnum;

}

public int totalRowsInSheet(int sheetNum)
{
int TotalRows=sht.getLastRowNum()-sht.getFirstRowNum();
return TotalRows;

}


}

****************END Actual methods***********************

M
Replied on 19/01/2016

I debugged the programe n found out that i had wrong condition in second for loop it should be less than or equal to one or LastColumnNumber.


-------------------------------------

but I still get NULL pointer Exception for below lines Responsive image

Please help.

nt s=xl.totalRowsInSheet(0);
System.out.println(s);
//int totalrows=xl.totalRowsInSheet(0);
----------------------------------------------------
for(int i=2;i<13;i++){

for(int j=0;j<=1;j++){
System.out.println(xl.getdata(0, i, j));
}


M
Replied on 20/01/2016

Hi Ashish,

I got the solution for this problem, but I really want to discuss about this problem.

Can we discuss it on skype?


M
Replied on 20/01/2016

Hi Ashish,

I have highlighted change in bold.
Please review the same.Is it a correct way or it can be done in some other way when using functions?


public class configxl {


HSSFSheet sht;
HSSFWorkbook wb;
HSSFRow row;
HSSFCell cell;
String data="";
FileInputStream file;
int totalrows;
String path;
[b:2ycjpemp] int sheetnum;[/b:2ycjpemp]

public configxl(String path) throws IOException


{
file = new FileInputStream(path);

wb = new HSSFWorkbook(file);

[b:2ycjpemp]sht = wb.getSheetAt(sheetnum);[/b:2ycjpemp]


}

/*public int getrowcount(int sheetn)
{
//totalrows=0;
wb.getSheetAt(sheetn);
totalrows= sht.getLastRowNum();
return totalrows;

}*/



public String getdata(int sheetn,int row,int col)
{
sht = wb.getSheetAt(sheetn);
cell= sht.getRow(row).getCell(col);
if(cell.getCellType()==cell.CELL_TYPE_NUMERIC)

{
String celltext=String.valueOf(cell.getNumericCellValue());
return celltext;
}

else{
if(cell.getCellType()==cell.CELL_TYPE_STRING)
data = cell.getStringCellValue();
return data;
}


}

public double NoofColumns(int sheetn,int rnum) throws IOException
{


int lastcellnum= sht.getRow(rnum).getLastCellNum();
return lastcellnum;

}

public int totalRowsInSheet(int sheetNum) throws IOException
{
//new configxl(path);
//sht = wb.getSheetAt(sheetNum);
int TotalRows=sht.getLastRowNum()-sht.getFirstRowNum();
return TotalRows;

}


}


M
Replied on 22/01/2016

it seems okay. is it working alright?


M
Replied on 23/01/2016

ya, its working ..thank u for review.