Search code examples
javaseleniumautomationcucumberui-automation

How to retrieve an id from the HashMap


I have the below script where I get the id correctly but when I append it to the url I am getting as

java null pointer Exception.

Below is the method I used to store the ids:

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()));     
} 

Then I call the above method in order to append the latestId to the url:

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

so in this case I fetch the id from the previous method by appending as above but when I do this it is not fetching the id and causes a null pointer Exception.

Any inputs on what I can do to get this resolved.


Solution

  • basically createdValue and getid both are static, so when you are calling it like this :

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

    this is getting called :

    public static HashMap<String, String> createdValue; 
    

    and since it does not have anything, you are getting the null pointer exception.

    Also, I think if you call this :

    getid first and then calling like this :

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

    should help you by past this issue.