Null Attribute Error

I have one endpoint which will return response in below format:
{
“items”:[ { …}],
“nextPage”:27ubdkj
}
If “items” array length is within 10, then “nextPage” value will be null.
{
“items”:[ { …}],
“nextPage”:null
}

In gatling script, I am extracting the “nextPage” value using check method:
.check(jsonPath(“$.nextPage”).optional.saveAs(“basicNextPage”))

And, in my scenario, I have do while loop asserting whether “nextPage” value is exists or not and based on that executing another request:
exec(getAllEntitlements)
.doWhile(“${basicNextPage.exists()}”) {
exec(getAllEntitlementsAndPage)
}
While running this script, I am getting error “[ERROR] i.g.h.a.HttpRequestAction - ‘httpRequest-33’ failed to execute: Attribute basicNextPage’s value is null”.

How to fix this?

Beware that exists() only checks for the existence of the key, even if the value is null.
You probably have to write a more complex test condition with the Session API.

doWhile(session => ???)

I tried having a query like this :
doWhile(session => session("basicNextPage").asOption[String].isDefined) and
asLongAs(session => session("basicNextPage").asOption[String].isDefined)
But still getting same error “[ERROR] i.g.h.a.HttpRequestAction - ‘httpRequest-33’ failed to execute: Attribute basicNextPage’s value is null”.

Can you provide me some example to fix this issue?

If you struggle with Option, I guess you struggle with scala in general. You know you can write Gatling Simulation in java?

To answer you specific question:

  doWhile(session => Option(session("basicNextPage").as[String]).isDefined)

Cheers!

I have used the above condition, but still facing the error
Condition evaluation crashed with message 'j.l.ClassCastException: Attribute basicNextPage's value is null', exiting loop

Code snippet:

asLongAs(session => Option(session("basicNextPage").as[String]).isDefined)

I have provided the httprequestbuilder which I have been using:

  val getAllEntitlements: HttpRequestBuilder = getEntitlementByAccount("entitlement_GetAllEntitlements","")
    .check(jsonPath("$.items").notNull)
    .check(status is 200)
    .check(jsonPath("$.nextPage").optional.saveAs("basicNextPage"))

  val getAllEntitlementsAndPage: HttpRequestBuilder = getEntitlementByAccount("entitlement_GetAllEntitlementsAndPage","page=${basicNextPage}")
    .check(jsonPath("$.items").notNull)
    .check(status is 200)
    .check(jsonPath("$.nextPage").optional.saveAs("basicNextPage"))

Also provide the scenario in which way I have constructed calling APIs:

exec(getAllEntitlements)
  .asLongAs(session => Option(session("basicNextPage").as[String]).isDefined) {
    exec(getAllEntitlementsAndPage)
  }

Please provide some solution to fix this issue!

Ok, my bad, I didn’t test the case for JSON null type.

So, you can use the session.attributes Map to be able to get your value.

asLongAs(session => session.attributes.get("basicNextPage").filterNot(_ == null).isDefined)

or shorter:

asLongAs(session => !session.attributes.get("basicNextPage").forall(_ == null))

Cheers!

Thank You @sbrevet for your support in solving this issue.