java.lang.NullPointerException: Cannot invoke method contains() on null object

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

Relevant lines of code:

String noteTitle = CustomKeywords.'custom.ff.selectUnreadEducatorsNotes'()

readStatus = CustomKeywords.'custom.ff.getEducatorsNoteReadStatus'(noteTitle)

assert readStatus.contains('Read')

assert !(readStatus.contains('Unread'))

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):

	@Keyword
	def String getEducatorsNoteReadStatus(String title) {
		WebDriver driver = DriverFactory.getWebDriver()
		List<WebElement> 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)) {
				return note.findElement(By.xpath("span[@class='smallcomments']")).getText()
			}
		}

		throw new Exception("Null object")
	}

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)

image

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?

Thanks :slight_smile:

Try…

and

String readStatus = CustomKeywords.'custom.ff.getEducatorsNoteReadStatus'(noteTitle) 

Thanks @Russ_Thomas for the response! I tried what you suggested, it still returned the same error message I’m afraid

String noteTitle = CustomKeywords.'custom.ff.selectUnreadEducatorsNotes'()
println noteTitle

What does that print in the console?

It returns this error msg:

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)

Well, THAT is your problem. There is something wrong with your CustomKeywords package/class/file.

Who wrote it? Does any method in that package work?

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

	throw new Exception("Null object")
}

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

You have declared/defined noteTitle as a String more than once < just do it ONCE.

The FIRST time you use noteTitle, type String noteTitle. All the other times, just type noteTitle.

Thank you! I will try your suggestions and let you know

Oh sorry, before your suggestions I had that line down as
String readStatus = CustomKeywords.'custom.ff.getEducatorsNoteReadStatus'(noteTitle)

Then changed it to this as you suggested above:

String noteTitle = CustomKeywords.'custom.ff.selectUnreadEducatorsNotes'()
println noteTitle

That’s why it said I declared it twice. I’ve now tried this instead:

String readStatus = CustomKeywords.'custom.ff.getEducatorsNoteReadStatus'
println readStatus

…and I get the ‘MissingPropertyException’ error because it changes to a ‘binary statement’: image

The package itself is fine.

Turns out it needs the parameters after, so I’ve tried this now:

String readStatus = CustomKeywords.'custom.ff.getEducatorsNoteReadStatus'(noteTitle)
println(readStatus)

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

Thanks for the help so far I appreciate it

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. :wink:

Ah that is true. I have added a try/catch exception thanks :grinning:

Yes good point, would help if I could actually write code though lol