【问题标题】:Calling methods in java (Selenium/Apache-POI included)在 Java 中调用方法(包括 Selenium/Apache-POI)
【发布时间】:2016-04-12 21:03:50
【问题描述】:

我创建了一个类“ReadURL”,用于获取 excel 中存在的 URL。

public class ReadURL
{
static String val;
public String readExcel(String path,String tab,int rowNum,int cellNum) throws Exception
{
      FileInputStream fis = new FileInputStream(path);
      Workbook wb = WorkbookFactory.create(fis);
      Sheet sh = wb.getSheet(tab);    
      Row row = sh.getRow(rowNum);
      String val = row.getCell(cellNum).getStringCellValue();
      return val; 
}
}

我通过声明值从另一个具有 main 方法的类 'DeadLinks' 调用上述方法。

public class DeadLinks 
{
public static void main(String[] args) throws Exception
{
    String addr = "C:/Users/sjois/Documents/Automation/TestURLs-Dev.xlsx";
    String sht = "USpages";
    int r = 2;
    int c = 0;
    ReadURL url = new ReadURL();
    url.readExcel(addr, sht, r, c);
    System.out.println(ReadURL.val);
}
}

当我使用 main 方法执行“ReadURL”类并对值进行硬编码时,一切正常。但是,当我尝试从另一个函数调用该函数时,它总是返回“Null”。在编译或运行时我没有收到任何错误。 我哪里错了?

【问题讨论】:

  • 这两个类都在同一个包中,我也在导入所有必需的 Apache-poi 包。

标签: java excel selenium apache-poi


【解决方案1】:

我的错!在全球和本地两次宣布“val”! 删除了本地声明,现在完美运行! :)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-25
    • 2020-04-26
    • 1970-01-01
    相关资源
    最近更新 更多