【问题标题】:How to retrieve an id from the HashMap [duplicate]如何从 HashMap 中检索 id [重复]
【发布时间】: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 并导致空指针异常。

关于我可以做些什么来解决这个问题的任何意见。

【问题讨论】:

  • 请格式化您的问题。检查How do I format my posts using Markdown or HTML?
  • 你总是在你的getid 方法中创建一个新的HashMap 真的是故意的吗?我怀疑你应该在你声明它的地方初始化你的静态哈希映射。它必须在您调用任何其他方法之前对其进行初始化。
  • 只需运行该方法,看看System.out.println(createdValue.put("latestDataId", latestId.getText())); 这一行有什么内容?
  • 感谢您的回复,但您能否提供更多意见
  • 在这一行中,唯一可以导致NullPointerException 的是地图本身 - createdValue - 是null。所以你可能在初始化它之前就到了那里。你没有告诉我们你在哪里调用getid(),所以我不能确定。只需使用调试器单步调试您的代码,或添加一些调试输出以查看发生了什么。

标签: java selenium automation cucumber ui-automation


【解决方案1】:

基本上createdValuegetid 都是静态的,所以当你这样称呼它时:

newurls.add(confirmationURL + "&id=" + createdValue.get("latestDataId")); 

这被调用了:

public static HashMap<String, String> createdValue; 

因为它没有任何东西,所以你得到了空指针异常。

另外,我想如果你这样称呼:

getid,然后像这样调用:

String confirmationURL = "https://test-webapp.net/#/type=confirm";
List<String> newurls = new ArrayList<String>();
getid(driver);
newurls.add(confirmationURL + "&id=" + createdValue.get("latestDataId")); 

应该可以帮助您解决这个问题。

【讨论】:

  • -我试图按照你所说的给出,但是当我运行整个脚本时,它会导致过时的元素,它是 bz 该方法有 lastetid.click 和 createdValue.get("latestDataId"));我需要这两行 bz 在一个屏幕中单击 id 并在另一个屏幕中将 id 附加到 url 我可以输入我需要做什么才能在一种方法中使用相同的方法这些地方
  • 此票仅针对空指针异常,已解决。我看不出不接受的意义。据我所知,您已经为陈旧的元素参考创建了一张新票,对吗?我们不鼓励人们不接受已经接受的票,如果它解决了他们的查询。此外,您应该在每个帖子中问一个问题。
猜你喜欢
  • 2017-06-06
  • 1970-01-01
  • 2020-06-05
  • 2014-08-25
  • 1970-01-01
  • 1970-01-01
  • 2021-11-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多