Java Selenium Webdriver-引用非静态的JEXEL字符串作为静态
我编写了一个数据驱动的脚本,由使用jexcelapi读入的Excel文档提供,现在我想将我的变量分离到一个单独的类中,但收到了“不能对非静态字段进行静态引用”错误 你能告诉我怎么做吗?下面显示了“variable”类的示例Java Selenium Webdriver-引用非静态的JEXEL字符串作为静态,java,selenium,webdriver,Java,Selenium,Webdriver,我编写了一个数据驱动的脚本,由使用jexcelapi读入的Excel文档提供,现在我想将我的变量分离到一个单独的类中,但收到了“不能对非静态字段进行静态引用”错误 你能告诉我怎么做吗?下面显示了“variable”类的示例 //jexcel sheet/cell reference Cell btcell0 = sheet.getCell(6, 2); //variable name required to be static String tNbuyName = btcell0.getCont
//jexcel sheet/cell reference
Cell btcell0 = sheet.getCell(6, 2);
//variable name required to be static
String tNbuyName = btcell0.getContents();
我看到的示例允许我引用另一个类中的变量,该类产生“Cannotmakestaticreferencetononstaticfield”错误
不能将非静态值指定给静态类字段。从中获取值的方法应该是静态方法,或者字段tNbuyName应该是非静态的
要么将tNbuyName设置为非静态,要么创建一个静态实用程序方法来获取值并将其分配给tNbuyName。大家好,谢谢你们回到我这里,我对这一点的理解越来越清楚,但我还是有点不清楚 这是我在“ExcelUtils”类中使用的方法
public static void setExcelFile(String Path, int Sheetname) throws Exception {
try {
Workbook workb = Workbook.getWorkbook(new File(Path));
Sheet = workb.getSheet(Sheetname);
} catch (Exception e) {
throw (e);
}
}
public static String getCellData(int ColNum, int RowNum) throws Exception{
try {
Cell = Sheet.getCell(ColNum, RowNum);
String CellData = Cell.getContents();
return CellData;
} catch (Exception e) {
return "";
}
}
接下来,在“ResultsMap”类中,我将为变量赋值
public class ResultsMap {
public static void WriteTo() throws Exception {
String tNbuyName = ExcelUtils.getCellData(6, 2);
String tNbuyPass = ExcelUtils.getCellData(6, 3);
String tAccName = ExcelUtils.getCellData(6, 4);
String tCasNo = ExcelUtils.getCellData(6, 5);
etc, etc......
但是现在在AdminBUser类中,我想访问ResultsMap中的变量,但是我该怎么做呢
例如,我一直在尝试System.out.println(ResultsMap.WriteTo(tNbuyName)
但会显示一条消息,提示tNbuyName无法解析为变量
这是目前的文件结构
public class ResultsMap {
public static void WriteTo() throws Exception {
String tNbuyName = ExcelUtils.getCellData(6, 2);
String tNbuyPass = ExcelUtils.getCellData(6, 3);
String tAccName = ExcelUtils.getCellData(6, 4);
String tCasNo = ExcelUtils.getCellData(6, 5);
etc, etc......