Can't figure out Null Pointer error in Action Generator

Hello everyone,

I’ve been writing a code that simulates the decay of a Rubidium-88 ion, but I can’t seem to solve this error while running the application:

Here is the code for the action generator, if anyone would be able to help locate where exactly the error in the code is, because I can’t seem to find it.

#include "PrimaryActionGenerator.hh"
#include "G4ParticleTable.hh"
#include "G4IonTable.hh"
#include "G4SystemOfUnits.hh"
#include "G4RandomDirection.hh"

  // Create a new particle gun
  fParticleGun = new G4ParticleGun();

  //define Rb88 properties
  G4int Z = 37, A = 88;
  G4double ionCharge = 37.*eplus;

  // Set the particle type to Rubidium-88
  G4ParticleDefinition *ion = G4IonTable::GetIonTable()->GetIon(Z, A, 0);


  // Set the particle energy to the Q value of Rubidium-88 decay (2.018 MeV)
  G4double energy = ion->GetPDGMass() - 2.018 * MeV;

  // Set the particle position to the center of the detector
  fParticleGun->SetParticlePosition(G4ThreeVector(0, 0, 0));

  // Set the particle direction randomly

  delete fParticleGun;

void PrimaryActionGenerator::GeneratePrimaries(G4Event* anEvent)
  // Generate one primary particle for each event

I appreciate the time and help!

At the time when the constructor of PrimaryGeneratorAction is invoked, the G4IonTable is not yet built. So ion is a null pointer. You must move the definition of ion in GeneratePrimaries().
See examples basic/B3 or extended/radioactivedecay/rdecay01