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(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 (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.

Cheers,

Pierre

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