Ccustom Keyword creates nullpointer, same code in cucumber-step passes no problem?

Hi,

I think I am missing something here:

I am using custom keywords to reuse code for handling lists and tables in our application.

The code works, however when I use the keyword I always get a nullpointer and it fails the cucumberstep in the log:
(*But the code actually works even when the nullpointer is thrown - the main issue is that the step fails *)

Reason:
java.lang.NullPointerException: Cannot invoke method call() on null object
	at kalliope.KAL_POST.openFirstPoststukinTable(KAL_POST.groovy:127)
	at ✽.I open the first 'poststuk' in the list and verify case acces(C:/Users/knightrider/Katalon Studio/abbKatalonStudio/Include/features/Kalliope/KAL_POST.feature:149)

Since the console did not show exactly where in the keyword the nullpointer was thrown, I decided to copy the code directly into my cucumber step to see if I could debug that way.

Strangely enough the nullpointer is gone when not using the keyword to handle the list and the cucumber step is passed, the only difference I see is in the console.

This is the code in question:, which is copied directly from the keyword into the cucumberstep.

//listHandler.clickFirstCaseInList()()

        wait.forKALLoader()
		
		WebDriver driver = DriverFactory.getWebDriver()
		
		WaitForPageLoadKeyword
				
		List<WebElement> tables = driver.findElements(By.id('searchResultsGrid'))
		WebElement searchResultsGrid = tables.get(tables.size()-1)
		WebElement Table = searchResultsGrid.findElement(By.tagName('tbody'))
		List<WebElement> Rows = Table.findElements(By.tagName('tr'))
		List<WebElement> Cells = Rows.get(0).findElements(By.tagName('td'))
		
		WebElement FileName = null 
		WebElement x = null 
		
		for(int i=0; i<Cells.size(); i++) {
		
			try {
				x = Cells.get(i).findElement(By.className('case'))
		
				if(x != null) {
					FileName = x
		
					break
				} else {
					println 'element is not found'					
				}
			} catch (e) {
				//e.printStackTrace()
			}
		}
		
		wait.andClick(WebUI.convertWebElementToTestObject(FileName), 5)		

I could just use the code as is, but that kind of defeates the use of the cutstom keywords.
Hopefully I can learn something here.

1 Like

Hi @alexander.boterberg, thank you for sharing this information with us. Let me check with our internal team for more details. Cheers!

1 Like

Hey @alexander.boterberg, just checking with the team and actually with custom keyword we are unable to help with. However, let’s wait for other community experts in this forum to try. Hope you find your solution soon. Best!

1 Like

@alexander.boterberg

Your problem may be clear to you, but not to others. Please understand that the information you showed to us is not enough for others to reproduce your problem on our side. Unless the problem reproduced, it is impossible for others to make any comment about your problem.

1 Like

I’m not sure what I should add more to make this issue more understandable, but here it goes?

It is a Nullpointerexception that gets thrown by the code block I pasted.

It should open the first case in a table, however the way the application works, there are different tabs within one page. When I open the second tab within the page, a second table is loaded into the html, a third tab a third table etc. (There are max 4 tabs in one page as of today) → thats the part where I go tables.size()-1 so I always get the latest loaded table. (because each table has the same ID…)

As far as I know there are no issues there.

Then I continue identifying parts of the table, and start looking for a cell that contains a ‘case’
The case is the link I have to click to open the document.

Now, like I mentioned, when I paste this code into the cucumber step there is no issue no error.

But when I want to use the code as a custom keyword, It throws the exception.

The exception itself only refers to the cucumber step and not the actual custom keyword code block, which I find strange but other than that I could not identify anything strange.
That is when I decided to reach out to the community.

Also, I am not sure, but I don’t think you will be able to paste the code to reproduce this locally:

In the meantime I have tried this keyword on a different case, where it does work without the nullpointer, so now I’m guessing the issue lies somwhere else…

1 Like

Sorry, I am not able to follow your issue.

1 Like

No problem, thank you for looking at it.

1 Like