2

I am trying to read Data from Excel file and input data to my web application but its not working.

Please have a look at my code.This is the class created for excel

I used Apache POI

Excel class

package ReadExcel;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

public class Excel_Data {
    HSSFWorkbook  wb;
    HSSFSheet sheet1;
    public Excel_Data(String fileName,String sheetName) throws Exception {
        File src= new File("E:\\Jino_testing\\Git_Hub\\Zmarta_se\\resources\\Zmarta.xls");
        FileInputStream  fis = new FileInputStream(src);
        HSSFWorkbook wb = new HSSFWorkbook(fis);
    }

    public  String getData( int  sheetNumber, int row,int column) { 
        HSSFSheet sheet1 =wb.getSheetAt(0);
        String data =sheet1.getRow(row).getCell(column).getStringCellValue();
        return  data;
    }
}

This is my Test class which I am running

public void ApplyLoan() throws Exception {
     //Reading Excel  
     Excel_Data read = new  Excel_Data("Zmarta.xls","Co-Applicant" );
     Thread.sleep(3000);
     driver.findElement(apply).click();
     driver.findElement(loan).click();
     Thread.sleep(7000);
     driver.findElement(amount).sendKeys(read.getData(0,1,1));
     Thread.sleep(3000);
     new Select (driver.findElement(years)).selectByVisibleText("14 år");
 }
jino
  • 69
  • 1
  • 12
  • 1
    What does "not working" means? Any errors? Which? Where? Btw.: You are declaring class members `HSSFWorkbook wb` and `HSSFSheet sheet1` but don't using them but do new declaring them in the methods. – Axel Richter Feb 07 '18 at 11:58
  • @AxelRichter Not working means not reading data ERROR--ava.lang.NullPointerException at ReadExcel.Excel_Data.getData(Excel_Data.java:41) at Pageobject.Locators.ApplyLoan(Locators.java:138) – jino Feb 07 '18 at 12:11
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Francis Bartkowiak Feb 07 '18 at 12:22
  • 1
    As said, you should using your declared class members. So `HSSFWorkbook wb = new HSSFWorkbook(fis);` should be `this.wb = new HSSFWorkbook(fis);` in `Excel_Data` constructor. Else `wb` is `NULL` in `getData` method. – Axel Richter Feb 07 '18 at 12:30
  • @jino There are various errors in your code...share complete code so that I can update it. – Sodium Feb 07 '18 at 17:23
  • @GauravGenius You can find in my repo https://github.com/jino555 – jino Feb 08 '18 at 04:12
  • @GauravGenius Thanks Bro, it worked, will need your help in future also – jino Feb 12 '18 at 04:53
  • You are welcome. You can accept the answer if it helped you. – Sodium Feb 12 '18 at 04:57
  • @GauravGenius Iam getting an error """java.lang.IllegalStateException: Cannot get a STRING value from a NUMERIC cell"""" Also if possible can u explaoin the code in Excel_data class – jino Feb 12 '18 at 06:38
  • @GauravGenius Is there any simple code for reading excel data and also Iam getting error on method getRawValue(); – jino Feb 12 '18 at 08:48
  • @GauravGenius I tried to read next sheet and I used "1" for next sheet driver.findElement(cossn).sendKeys(read.getData(1, 2, 2)); but its not reading.what can be the reason – jino Feb 13 '18 at 10:08
  • @jino is there any error? paste it here – Sodium Feb 13 '18 at 10:30
  • @GauravGenius No Gaurav there is no error showing . Can u please have a look into my code Repo-> github.com/jino555 – jino Feb 13 '18 at 10:56
  • @jino I can see in your repo... `driver.findElement(cossn).sendKeys(read.getData(0, 2, 2));` this call is for sheet at 0 index. modify it with `driver.findElement(cossn).sendKeys(read.getData(1, 2, 2));` and make sure there is some data in corresponding row, colum – Sodium Feb 13 '18 at 11:03
  • @GauravGenius soory i forgot to update the code in github , i added sheetindex as 1 but not working – jino Feb 13 '18 at 11:25
  • @jino print value returned by function `System.out.println(read.getData(1, 2, 2))` verify whether it is blank, if it is blank check sheet(1) in your excel file whether it contains data @row(2) column(2) – Sodium Feb 13 '18 at 11:36
  • @GauravGenius i checked it coming blank and data is available in sheet(2) row(2) column(2) – jino Feb 13 '18 at 13:21
  • @GauravGenius U can find my excel sheet in resources folder in github – jino Feb 14 '18 at 05:32
  • @jino ..I've gone through your excel sheet...in sheet there is only one cell which has data which is @ 0th row, 5th column, contains `ssn`. which I can successfully print with `System.out.println(read.getData(1, 0, 5));` So basically `System.out.println(read.getData(1, 2, 2));` cell is blank that's why it is printing blank. – Sodium Feb 14 '18 at 17:28
  • @GauravGenius thanks its working, This is a client project , Whats your opinion on the code , Is it a standard one ,any suggestions or improvments – jino Feb 15 '18 at 06:00
  • @jino ..welcome..one thing you can improve..put locators in separate file i.e. `Keyword Driven` approach to make your code more readable...If resolution helped, you can accept my answer ! – Sodium Feb 15 '18 at 06:53
  • @GauravGenius Thanks Ok , I will try with keyword drivem ( storing all locators in a separate file) Now, i have used POM Concept , created a separte class "Locators" for strong all locators and methods of it and a base class for calling that methods.....so using POM Concept is a standard one or not? – jino Feb 15 '18 at 07:09
  • @jino yes it is a standard..by the way accept my answer so that other users can trust it – Sodium Feb 15 '18 at 07:32
  • @GauravGenius yes sure , i will use it – jino Feb 15 '18 at 07:42
  • @GauravGenius donee – jino Feb 15 '18 at 09:16
  • @jino I can't see answer accepted checkbox, have you really accepted? – Sodium Feb 15 '18 at 13:48
  • @GauravGenius i have done already,please check it – jino Feb 20 '18 at 09:51
  • @GauravGenius Hi Gaurav, have a doubt, Can we call 2 separate classes in a single class by creating object – jino Mar 01 '18 at 05:44

1 Answers1

1

There are various errors in your code, here is the corrected code...

Here is your Excel_Data class

public class Excel_Data {
    HSSFWorkbook  wb;
    HSSFSheet sheet1;
    HSSFCell cell;

    public Excel_Data(String fileName) throws Exception {

        FileInputStream  fis = new FileInputStream(fileName);
        wb = new HSSFWorkbook(fis);
    }

    public  String getData(int sheetNumber, int row, int column) { 

        sheet1 = wb.getSheetAt(sheetNumber);
        String data = "";
        try{
            cell = sheet1.getRow(row).getCell(column);
            switch(cell.getCellTypeEnum()) {
                case BOOLEAN:
                    data = cell.getRawValue();
                    break;
                case NUMERIC:
                    data = cell.getRawValue();
                    break;
                case STRING:
                    data = cell.getStringCellValue();
                    break;
                default:
                    break;
            }

        }catch(Exception e){
            e.printStackTrace();
        }
        return data;
    }
}

Here is Test class

public void ApplyLoan() throws Exception {
    //Reading Excel  
    Excel_Data read = new Excel_Data("E:\\Jino_testing\\Git_Hub\\Zmarta_se\\resources\\Zmarta.xls");
    Thread.sleep(3000);
    driver.findElement(apply).click();
    driver.findElement(loan).click();
    Thread.sleep(7000);
    driver.findElement(amount).sendKeys(read.getData(0,1,1));
    Thread.sleep(3000);
    new Select (driver.findElement(years)).selectByVisibleText("14 år");
}

Note -> HSSFWorkbook is to read from .xls file... use XSSFWorkbook to read from .xlsx file.

Sodium
  • 1,016
  • 1
  • 9
  • 22
  • @jino, You need to specify which type of data you are going to read from cell. Editing my answer – Sodium Feb 12 '18 at 08:04
  • @jino, follow changes in undated answer – Sodium Feb 12 '18 at 08:10
  • @jino, this is very simple code to read excel data, logically to read from excel, you have to input excel file name, then you have to find workbook, then you have to go to cell to read actual data, which could be of various data types...that's all there in code nothing else...so it's simple. Further if you can use single function instead of two...and if you want to invoke test for each rown in excel, you can use `@DataProvider` testNG class. – Sodium Feb 12 '18 at 09:35
  • Gaurav ...Iam getting error on method getRawValue(); – – jino Feb 12 '18 at 10:22
  • @jino, see my answer to [This post](https://stackoverflow.com/questions/48683878/how-to-read-a-numeric-value-i-e-double-value-from-excel-sheet-using-apache-po/48693334#48693334)..this include complete script to read data from excel sheet with `@DataProvider` testNG. – Sodium Feb 12 '18 at 12:35