简体   繁体   中英

Unable to call function from another java class

I am developing some selenium code and was trying to get input from an Excel sheet using apache POI. So far I have managed to get the input but I am unable to transfer it from class to class. Please see the code below:

Functions To Be Called:

package Excel;

import java.io.File;
import java.io.FileInputStream;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class Read {

    XSSFSheet Names;
    public void read() throws Exception{

        File src = new File("C:\\Users\\dindo\\Documents\\tests\\d2c-lv-int-01_DATA.xlsx");

        FileInputStream fis = new FileInputStream(src);

        XSSFWorkbook wb = new XSSFWorkbook(fis);

        Names = wb.getSheetAt(0);

    }

    public void getcell(int row, int col){
        String stringresult = Names.getRow(row).getCell(col).getStringCellValue();
        String intresult = Names.getRow(row).getCell(col).getStringCellValue();
    }

Trying To Call The Functions:

package Pages;

import Excel.Read;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;

public class DetailsPage {
    Read cel = new Read();
    cel.getcell(2,4)

    @FindBy(how = How.XPATH, using = "/*xpath*/")
    public WebElement coveramountelement;

    public String coveramount = cel.intresult;

    public void EnterDetails() {
        coveramountelement.sendKeys(coveramount);
    }
}

错误

All errors that are for line 10 are for cel.getcell(2,4);

I would suggest restructuring your code like so

Functions are meant to be reused - so hard-coding specific variables into them isn't a good idea. And you're not storing any state, so just return the objects you want

public class Read {

    public static XSSFSheet getSheet(String file, int sheetIndex) throws Exception{
        File src = new File(f);
        FileInputStream fis = new FileInputStream(src);
        XSSFWorkbook wb = new XSSFWorkbook(fis);
        return wb.getSheetAt(sheetIndex);
    }

    public static Cell getCell(XSSFSheet s, int row, int col) {
       return s.getRow(row).getCell(col);
    }

Now, use those generic functions to do your specific tasks

public class DetailsPage {
    private XSSFSheet names;

    public DetailsPage() {
        try {
           names = Read.getSheet("C:\\Users\\dindo\\Documents\\tests\\d2c-lv-int-01_DATA.xlsx", 0);
        } catch (Exception e) {
            e.printStackTrace();
        } 
    }

    @FindBy(how = How.XPATH, using = "/*xpath*/")
    public WebElement coverAmountElement;

    public void enterDetails() {
        if (names != null) {
            XSSFCell c = Read.getCell(names, 2,4);
            String coveramount = c.getStringCellValue();
            coverAmountElement.sendKeys(coveramount);
        }
    }
}

You cannot call a method/function outside of another method/function unless it's to initialize a variable. Furthermore, the getCell method returns a void which means that you can't use it to assign to anything, Also, you're missing a semicolon. So change this

Read cel = new Read();
cel.getcell(2,4)

To something like this:

package Pages;

import Excel.Read;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;

public class DetailsPage {

    @FindBy(how = How.XPATH, using = "/*xpath*/")
    public WebElement coveramountelement;


    public void EnterDetails() {
        Read cel = new Read();            
        String coveramount = cel.getcell(2,4);
        coveramountelement.sendKeys(coveramount);
    }
}

And for getCell have it be something like this:

public String getcell(int row, int col){
    String stringresult = Names.getRow(row).getCell(col).getStringCellValue();
    return stringresult;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM