Cosmic beam ion/nuclei definition error

Hi everyone. I once again come to you for guidance. I’m trying to create a cosmic beam with a lot of different particles and ions as the primary particlegun. I’ve managed to find out that everytime any ion/nuclei is produced my simulation crashes.

And the error I get is from file line 102 :
if(!aParticleDefinition) { [G4Exception](“G4ParticleGun::SetParticleDefinition()”,“Event0101”, [FatalException],“Null pointer is given.”)

So i’m wondering if i may be defining them wrong or what i did wrong because I can’t seem to find my mistakes.

Thanks a lot, feel free to ask if you need more information.

PS : PrimaryGeneratorAction file is linked (22.6 KB)

Don’t do the ion-table fetches outside of a function. That means they’re getting called before the ion table is even filled. Do it in GeneratePrimaries. The particle definitions are singletons anyway, you’re not saving anything by pre-instantiating them, even if that worked.

It wasn’t about saving anything but for clarity of code but anyways thanks a lot for the fix.