【发布时间】:2021-10-24 14:09:24
【问题描述】:
我有下面的脚本,我可以正确获取 id,但是当我将它附加到 url 时,我得到的是
java空指针异常。
下面是我用来存储id的方法:
public static HashMap<String, String> createdValue;
public static void getid(WebDriver driver) throws InterruptedException
{
String pendingconfirm = buttonXpath_Replace.replace("XXXX", "Pending confirm");
clickOnButton(driver, pendingconfirm);
createdValue = new HashMap<String, String>();
List<WebElement> tableValues = driver.findElements(By.xpath("//table//tr//td[contains(@class,'mat-column-demandId')]//span"));
int tableValueSize = tableValues.size();
System.out.println("Get the no of rows:"+tableValueSize);
WebElement latestId = driver.findElement(By.xpath("(//table//tr//td[contains(@class,'mat-column-demandId')]//span)["+tableValueSize +"]"));
System.out.println("Latest DemandIds: "+ latestId .getText());
WebDriverWait wait = new WebDriverWait(driver,30);
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("(//table//tr//td[contains(@class,'mat-column-demandId')]//span)["+tableValueSize +"]")));
createdValue.put("latestDataId", latestId.getText());
System.out.println(createdValue.put("latestDataId", latestId.getText()));
}
然后我调用上述方法,以便将latestId 附加到url:
String confirmationURL = "https://test-webapp.net/#/type=confirm";
List<String> newurls = new ArrayList<String>();
newurls.add(confirmationURL + "&id=" + createdValue.get("latestDataId"));
所以在这种情况下,我通过按上述方式从前一个方法中获取 id,但是当我这样做时,它不会获取 id 并导致空指针异常。
关于我可以做些什么来解决这个问题的任何意见。
【问题讨论】:
-
你总是在你的
getid方法中创建一个新的HashMap真的是故意的吗?我怀疑你应该在你声明它的地方初始化你的静态哈希映射。它必须在您调用任何其他方法之前对其进行初始化。 -
只需运行该方法,看看
System.out.println(createdValue.put("latestDataId", latestId.getText()));这一行有什么内容? -
感谢您的回复,但您能否提供更多意见
-
在这一行中,唯一可以导致
NullPointerException的是地图本身 -createdValue- 是null。所以你可能在初始化它之前就到了那里。你没有告诉我们你在哪里调用getid(),所以我不能确定。只需使用调试器单步调试您的代码,或添加一些调试输出以查看发生了什么。
标签: java selenium automation cucumber ui-automation