# Null pointer when using randomSwitch in a chain

**URL:** <https://community.gatling.io/t/null-pointer-when-using-randomswitch-in-a-chain/2131>\
**Category:** Gatling (Open-Source)\
**Created:** [March 21, 2015, 12:33am UTC](https://community.gatling.io/t/null-pointer-when-using-randomswitch-in-a-chain/2131 "2015-03-21T00:33:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nadine\_Whitfield](https://avatars.discourse-cdn.com/v4/letter/n/d9b06d/32.png) [@Nadine\_Whitfield](https://community.gatling.io/u/Nadine_Whitfield)\
**Post date:** [March 21, 2015, 12:33am UTC](https://community.gatling.io/t/null-pointer-when-using-randomswitch-in-a-chain/2131/1 "2015-03-21T00:33:53Z")

</div>

I have a class that extends Simulation. This class is used to define individual POST requests from the api I am testing.

What I would like to do is create a chained sequence of 2-3 requests. Then there needs to be a final request that is randomly selected by the randomSwitch.

The switch contains 3 requests that are defined elsewhere within this class. Each has its own feeder calls, exec method, flow control and check statements and is assigned to a val object.

To run with Gatling, I have a _second_ Simulation class that sets up http protocol and injects load. It instantiates class members and serves as a generic ‘runner’ to exercise each predefined scenario.  
I’ve gotten this to work for each individual request in my class, as well as my sequence (until randomSwitch was added).

The switch looks like this \>

.randomSwitch (  
5.0 → exec(request3),  
10.0 → exec(request4),  
85.0 → exec(request5)  
)

It gets called right after 2 inline scenarios (each having its own .exec, .feed and .check statements) .  
The switch looks OK in the editor (no error indications), but when I try to run it, I get an error like this\>

Exception in thread “main” java.lang.NullPointerException

at io.gatling.core.structure.Execs$class.exec(Execs.scala:31)

at io.gatling.core.Predef$.exec(Predef.scala:33)

at performance.simulations.scenarios.PostRequests.(PostRequests.scala:167)

at performance.simulations.Injector.(Injector.scala:31)

at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)

at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)

at java.lang.reflect.Constructor.newInstance(Constructor.java:526)

at java.lang.Class.newInstance(Class.java:374)

at io.gatling.core.runner.Runner.run(Runner.scala:37)

at io.gatling.app.Gatling.start(Gatling.scala:236)

at io.gatling.app.Gatling$.fromMap(Gatling.scala:55)

at io.gatling.app.Gatling$.runGatling(Gatling.scala:80)

at io.gatling.app.Gatling$.runGatling(Gatling.scala:59)

at io.gatling.app.Gatling$.main(Gatling.scala:51)

at io.gatling.app.Gatling.main(Gatling.scala)

Is this not a legal use case? If so, what should I do to fix it?

A full code sample is attached.

Thanks in advance.

[gatling\_randomSwitchSample.txt](https://community.gatling.io/uploads/short-url/fmipxPKE1PFmY7tCUacFZe5wtPF.txt) (2.65 KB)

---

<div class="post-metadata">

**Author:** ![Pierre\_DAL-PRA](https://avatars.discourse-cdn.com/v4/letter/p/ecccb3/32.png) [@Pierre\_DAL-PRA](https://community.gatling.io/u/Pierre_DAL-PRA)\
**Post date:** [March 21, 2015, 12:53am UTC](https://community.gatling.io/t/null-pointer-when-using-randomswitch-in-a-chain/2131/2 "2015-03-21T00:53:07Z")

</div>

You need to define your vals before you use them.  
Just move your request before the point where you use them in your switch and it should be fine.

Cheers,

Pierre

---

<div class="post-metadata">

**Author:** ![Nadine\_Whitfield](https://avatars.discourse-cdn.com/v4/letter/n/d9b06d/32.png) [@Nadine\_Whitfield](https://community.gatling.io/u/Nadine_Whitfield)\
**Post date:** [March 21, 2015, 8:47pm UTC](https://community.gatling.io/t/null-pointer-when-using-randomswitch-in-a-chain/2131/3 "2015-03-21T20:47:15Z")

</div>

I rearranged the definitions as you mentioned and yes, the error went away. Thanks! I will keep that for future reference.

It turned out I could not use these predefined requests after all. The predefined feeder calls were overwriting session attributes that had been set by earlier requests in the chain.

To get around this, I ended up extracting and duplicating just the exec and .check portions of each request.  
Final version looked like this\>

.randomSwitch(  
5.0 →   
exec( http( “request3” )  
.post( “”"/path/to/resource""")  
.header(“userid”, “${userid}”)  
.headers(headers)  
.body(StringBody( { “”" { // omitted for brevity }"""  
} )).asJSON  
.check(status.is(201) )),

10.0 →   
exec( http(“request4”)  
.get( “”"/another/resource/path/me/${ProgramId}/status""")  
.header(“userid”, “${userid}”)  
.headers(headers)  
.check(status.is(200))  
),

85.0 →   
exec(  
http(“request5”)  
.post( “”"/path/gold/${id}""")  
.header(“userid”, “${userid}”)  
.headers(headers)  
.body(StringBody({"""{"// payload here } “”"  
})).asJSON  
.check(status.is(201) ))  
) // end switch
