Hi, I am trying to work out why my object returns as ‘null’, when I can see for myself that the result of this is not null, but should return text instead.
Error message:
java.lang.NullPointerException: Cannot invoke method contains() on null object
Here, the ‘null object’ is readStatus, which takes in noteTitle and calls a custom function on it. Here is the code from the second custom keyword I reference above (that readStatus takes in):
The result is that it invokes the exception “Null object” and I am not sure why. It should return the text on the page from the xpath span[@class=‘smallcomments’] which says “Unread” (as below)
There’s nothing wrong with the locator as it runs on another function successfully, so I’m just not sure why I am not getting this text returned?
groovy.lang.MissingPropertyException: No such property: custom.ff.getEducatorsNoteReadStatus for class: CustomKeywords
at TUP-1679_Trainee_Reads_Educators_Notes.run(TUP-1679_Trainee_Reads_Educators_Notes:64)
Its hard to say what the cause is without knowing your specific java implementation and the page under testing. If it were me I would put log messages in between each of the steps to display in the log what the values are. Id check title coming in to the function and the value of note about to be returned.
@Keyword
def String getEducatorsNoteReadStatus(String title) { // make sure title value is passed in the way you expect **
** System.out.println("The title value is: " + notes.getText());
WebDriver driver = DriverFactory.getWebDriver()
List notes = driver.findElements(By.xpath("//*[@id=‘appraisaloverview’]/div[3]/div[6]/p[1]/a[1]"))
for(WebElement note : notes) {
String actualTitle = note.findElement(By.xpath(“strong”)).getText()
if(actualTitle.equals(title)) { // if title is passed in correctly and it make it here then its probably not a xpath problem. im guessing you wont make it to here. **
** System.out.println("returning note: " + note.findElement(By.xpath(“span[@class=‘smallcomments’]”)).getText());
return note.findElement(By.xpath(“span[@class=‘smallcomments’]”)).getText()
}
}
Oh sorry, I typed readStatus instead of noteTitle. This is what it returns:
64: The current scope already contains a variable of the name noteTitle
@ line 64, column 8.
String noteTitle = CustomKeywords.'custom.ff.getEducatorsNoteReadStatus'
^
1 error
Still null object. Will get back to you after further troubleshooting. Will try the suggestions by @gfus69.
You can probably tell i didn’t write this myself… Just that the people who wrote it have left now and I’m the one who has taken over…bit unfortunate for them
also looking at that function more, is throwing a null exception and stopping program execution what u want if you cant find the correct read status? A try/catch exception would give u more control over an assert or error message without stopping the program.
Good luck and yeah its always fun taking over someone else code. Sometimes its better just to rewrite it your way than try and figure out there mess.