简体   繁体   English

无法从另一个Java类调用函数

[英]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. 我正在开发一些硒代码,并尝试使用apache POI从Excel工作表中获取输入。 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); 第10行的所有错误都针对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. 此外, getCell方法返回一个void ,这意味着您不能使用它来分配任何内容。此外,您还缺少分号。 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: 对于getCell ,它是这样的:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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