Can't Set Particle System TextureSheetAnimationModule.

I would like to set Particle System TextureSheetAnimationModule.rowIndex. Here’s the code based on Unity reference (Unity - Scripting API: ParticleSystem.TextureSheetAnimationModule.rowIndex):

ParticleSystem ps = particleEffect.GetComponent<ParticleSystem>();
ParticleSystem.TextureSheetAnimationModule tex = ps.textureSheetAnimation;
tex.rowIndex = 2;

It doesn’t work and generates this error:

NullReferenceException: Do not create your own module instances, get them from a ParticleSystem instance UnityEngine.ParticleSystem+TextureSheetAnimationModule.set_rowIndex (Int32 value)

Am I doing something wrong or is Unity reference out of date? I have Unity 5.5.2f1.

Edit: The Nullreference error shows up when GetComponent fails to get the desired component in the target object, that means you have not added a particle system to that object before playing the scene.

Also I am so lazy that I usually replace the type “ParticleSystem.TextureSheetAnimationModule” with “var”, even though many discourage this practice.