1

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.

cruisepandey
  • 28,520
  • 6
  • 20
  • 38
sandy
  • 27
  • 6
  • 1
    Please format you question. Check [How do I format my posts using Markdown or HTML?](https://stackoverflow.com/help/formatting) – seenukarthi Aug 24 '21 at 10:31
  • 1
    Is it really intentional that you always create a new `HashMap` in your `getid` method? I suspect that you should initialize your static hash map right where you declare it. It has to be initialized before you invoke any other methods on it. – Hulk Aug 24 '21 at 10:52
  • just run that method and see what is the out of this line `System.out.println(createdValue.put("latestDataId", latestId.getText())); ` ? – cruisepandey Aug 24 '21 at 10:57
  • thanks for the response but can you give more inputs – sandy Aug 24 '21 at 10:58
  • yes i ran that and it did fetch the latest id – sandy Aug 24 '21 at 10:58
  • the problem is when iam calling it in this place -newurls.add(confirmationURL + "&id=" + createdValue.get("latestDataId")); – sandy Aug 24 '21 at 10:59
  • this is the place where i get the null pointer it is basically not fetching the id – sandy Aug 24 '21 at 11:00
  • @Hulk can i get more inputs – sandy Aug 24 '21 at 11:08
  • 1
    in this line, the only thing that can lead to a `NullPointerException` is the map itself - `createdValue` - being `null`. So you probably get there before you initialized it. You have not shown us where you invoke `getid()`, so I cannot be sure. Just step through your code with a debugger, or add some debug outputs to see what is happening. – Hulk Aug 24 '21 at 11:08
  • ok so you are saying that before i call - newurls.add(confirmationURL + "&id=" + createdValue.get("latestDataId")); ---- i need to getID(Driver); – sandy Aug 24 '21 at 11:12
  • yes, because that is where you create the map. But actually, you should probably create the map somewhere else, e.g. statically when declaring `createdValue`. – Hulk Aug 24 '21 at 11:13
  • yes i added the line and it is working now thanks hulk for the help – sandy Aug 24 '21 at 11:19
  • is there anywhere i can upvote – sandy Aug 24 '21 at 11:19
  • @hulk i tried to give as you said but when i run the entire script it causes stale element it is bz the method has lastetid.click and also createdValue.get("latestDataId")); i would need both of these lines bz in one screen iam clicking the id and in another screen iam appending the id to the url can i get a input on what i need to do to have both in one method and use the same method in all the places – sandy Aug 25 '21 at 03:28

1 Answers1

1

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.

cruisepandey
  • 28,520
  • 6
  • 20
  • 38
  • -i tried to give as you said but when i run the entire script it causes stale element it is bz the method has lastetid.click and also createdValue.get("latestDataId")); i would need both of these lines bz in one screen iam clicking the id and in another screen iam appending the id to the url can i get a input on what i need to do to have both in one method and use the same method in all the places – sandy Aug 25 '21 at 03:28
  • This ticket is solely for null pointer exception, which was resolved. I don't see a point for unacceptance. As I am aware you already have a new ticket created for stale element reference, right ? We don't encourage folks to unaccept the already accepted ticket, if it solved their query. Also you should ask one question per post. – cruisepandey Aug 25 '21 at 04:06