# Null Attribute Error

**URL:** <https://community.gatling.io/t/null-attribute-error/8245>\
**Category:** Gatling (Open-Source)\
**Created:** [August 27, 2023, 12:09pm UTC](https://community.gatling.io/t/null-attribute-error/8245 "2023-08-27T12:09:31Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zuhair](https://avatars.discourse-cdn.com/v4/letter/z/f475e1/32.png) [@Zuhair](https://community.gatling.io/u/Zuhair)\
**Post date:** [August 27, 2023, 12:09pm UTC](https://community.gatling.io/t/null-attribute-error/8245/1 "2023-08-27T12:09:31Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![slandelle](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/slandelle/32/4_2.png) [@slandelle](https://community.gatling.io/u/slandelle)\
**Post date:** [August 27, 2023, 3:00pm UTC](https://community.gatling.io/t/null-attribute-error/8245/2 "2023-08-27T15:00:38Z")

</div>

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](https://gatling.io/docs/gatling/reference/current/core/session/session_api/).

`doWhile(session => ???)`

---

<div class="post-metadata">

**Author:** ![Zuhair](https://avatars.discourse-cdn.com/v4/letter/z/f475e1/32.png) [@Zuhair](https://community.gatling.io/u/Zuhair)\
**Post date:** [August 28, 2023, 5:02am UTC](https://community.gatling.io/t/null-attribute-error/8245/3 "2023-08-28T05:02:22Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![sbrevet](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/sbrevet/32/830_2.png) [@sbrevet](https://community.gatling.io/u/sbrevet)\
**Post date:** [August 28, 2023, 10:43am UTC](https://community.gatling.io/t/null-attribute-error/8245/4 "2023-08-28T10:43:52Z")

</div>

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:

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

```

Cheers!

---

<div class="post-metadata">

**Author:** ![Zuhair](https://avatars.discourse-cdn.com/v4/letter/z/f475e1/32.png) [@Zuhair](https://community.gatling.io/u/Zuhair)\
**Post date:** [August 28, 2023, 5:16pm UTC](https://community.gatling.io/t/null-attribute-error/8245/5 "2023-08-28T17:16:58Z")

</div>

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:

```auto
  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:

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

```

Please provide some solution to fix this issue!

---

<div class="post-metadata">

**Author:** ![sbrevet](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/sbrevet/32/830_2.png) [@sbrevet](https://community.gatling.io/u/sbrevet)\
**Post date:** [August 28, 2023, 6:54pm UTC](https://community.gatling.io/t/null-attribute-error/8245/6 "2023-08-28T18:54:23Z")

</div>

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.

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

```

or shorter:

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

```

Cheers!

---

<div class="post-metadata">

**Author:** ![Zuhair](https://avatars.discourse-cdn.com/v4/letter/z/f475e1/32.png) [@Zuhair](https://community.gatling.io/u/Zuhair)\
**Post date:** [August 29, 2023, 3:53am UTC](https://community.gatling.io/t/null-attribute-error/8245/7 "2023-08-29T03:53:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![slandelle](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/slandelle/32/4_2.png) [@slandelle](https://community.gatling.io/u/slandelle)\
**Post date:** [August 29, 2023, 7:54am UTC](https://community.gatling.io/t/null-attribute-error/8245/8 "2023-08-29T07:54:54Z")

</div>

Closed on August 29, 2023, 7:54am UTC


