Null pointer when using randomSwitch in a chain

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(

at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(

at java.lang.reflect.Constructor.newInstance(

at java.lang.Class.newInstance(








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 (2.65 KB)

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.



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>

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

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

85.0 →
.post( “”"/path/gold/${id}""")
.header(“userid”, “${userid}”)
.body(StringBody({"""{"// payload here } “”"
.check( ))
) // end switch