# Runtime exception during execution, leading to a crash

**URL:** <https://geant4-forum.web.cern.ch/t/runtime-exception-during-execution-leading-to-a-crash/11768>\
**Category:** Getting Started\
**Created:** [February 18, 2024, 7:41pm UTC](https://geant4-forum.web.cern.ch/t/runtime-exception-during-execution-leading-to-a-crash/11768 "2024-02-18T19:41:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![OdaiSuad](https://geant4-forum.web.cern.ch/user_avatar/geant4-forum.web.cern.ch/odaisuad/32/5987_2.png) [@OdaiSuad](https://geant4-forum.web.cern.ch/u/OdaiSuad)\
**Post date:** [February 18, 2024, 7:41pm UTC](https://geant4-forum.web.cern.ch/t/runtime-exception-during-execution-leading-to-a-crash/11768/1 "2024-02-18T19:41:11Z")

</div>

Please fill out the following information to help in answering your question, and also see [tips for posting code snippets](https://geant4-forum.web.cern.ch/t/how-to-post-code-snippets/9944). If you don’t provide this information it will take more time to help with your problem!

\_Geant4 Version: 11.1  
\_Operating System: Windows 10  
\_Compiler/Version: Microsoft Visual Studio Community 2022 (64-bit) - Version 17.9.0  
\_CMake Version: 3.25.1

Hello everybody,

Could anyone help me identify the following error? (Please see the attached snapshots.)

I am attempting to simulate the mass attenuation coefficient for various materials, such as alloys, glass, etc. No compilation issues were raised (see attached photo 1) . However, a runtime exception occurred during execution, leading to a crash (see the attached photo 2).

Please note that I can run the B1 example without any issues.

Is there any advice? Thank in advance

 ![Capture](https://geant4-forum.web.cern.ch/uploads/default/original/2X/a/a34f5cbe07bd19bdfba5ff21d95354dbd5ff6119.png)  
 ![VS](https://geant4-forum.web.cern.ch/uploads/default/original/2X/a/ab242042588284306dd853e2bf1a7e79242eda44.png)

---

<div class="post-metadata">

**Author:** ![bmorgan](https://geant4-forum.web.cern.ch/user_avatar/geant4-forum.web.cern.ch/bmorgan/32/7_2.png) [@bmorgan](https://geant4-forum.web.cern.ch/u/bmorgan)\
**Post date:** [February 19, 2024, 10:44am UTC](https://geant4-forum.web.cern.ch/t/runtime-exception-during-execution-leading-to-a-crash/11768/2 "2024-02-19T10:44:03Z")

</div>

The error message is telling you that a `nullptr` was passed to some call to `G4ParticleGun::SetParticleDefinition()`, so check for calls to this in your code and see how/why a `nullptr` is getting passed.

---

<div class="post-metadata">

**Author:** ![OdaiSuad](https://geant4-forum.web.cern.ch/user_avatar/geant4-forum.web.cern.ch/odaisuad/32/5987_2.png) [@OdaiSuad](https://geant4-forum.web.cern.ch/u/OdaiSuad)\
**Post date:** [February 19, 2024, 3:45pm UTC](https://geant4-forum.web.cern.ch/t/runtime-exception-during-execution-leading-to-a-crash/11768/3 "2024-02-19T15:45:22Z")

</div>

Thank you @bmorgan for your answer. I didn’t use G4ParticleGun::SetParticleDefinition() in my code. Maybe this is my mistake. I use the following function in my PrimaryGenerator.cc. Let me play with it and I will update you with my results.

```auto
PrimaryGeneratorAction::PrimaryGeneratorAction() {
    // Create particle gun
    particleGun = new G4ParticleGun(1);

    // Get particle table
    G4ParticleTable* particleTable = G4ParticleTable::GetParticleTable();
    G4String particleName;
    G4ParticleDefinition* particle = particleTable->FindParticle(particleName = "gamma");
    particleGun->SetParticleDefinition(particle);

```

####################################################

My PrimaryGeneratorAction.cc file is:

```auto
#include "PrimaryGeneratorAction.hh"
#include "G4Event.hh"
#include "G4ParticleGun.hh"
#include "G4ParticleTable.hh"
#include "G4SystemOfUnits.hh"

PrimaryGeneratorAction::PrimaryGeneratorAction() {
    // Create particle gun
    particleGun = new G4ParticleGun(1);

    // Get particle table
    G4ParticleTable* particleTable = G4ParticleTable::GetParticleTable();
    G4String particleName;
    G4ParticleDefinition* particle = particleTable->FindParticle(particleName = "gamma");
    particleGun->SetParticleDefinition(particle);
}

PrimaryGeneratorAction::~PrimaryGeneratorAction() {
    delete particleGun;
}

void PrimaryGeneratorAction::GeneratePrimaries(G4Event* anEvent) {
    // Set array of photon energies
    G4double photonEnergies[] = { 0.015, 0.1, 0.5, 1, 3, 5, 10, 15 }; // in MeV

    // Loop through each photon energy and generate primaries
    for (size_t i = 0; i < sizeof(photonEnergies) / sizeof(photonEnergies[0]); ++i) {
        G4double energy = photonEnergies[i] * MeV;

        // Set particle energy
        particleGun->SetParticleEnergy(energy);

        // Set particle position
        G4double positionX = 0.0;
        G4double positionY = 0.0;
        G4double positionZ = -5.0 * cm; // Position just above the source
        particleGun->SetParticlePosition(G4ThreeVector(positionX, positionY, positionZ));

        // Set particle direction
        G4double directionX = 0.0;
        G4double directionY = 0.0;
        G4double directionZ = 1.0;
        particleGun->SetParticleMomentumDirection(G4ThreeVector(directionX, directionY, directionZ));

        // Generate primary particle
        particleGun->GeneratePrimaryVertex(anEvent);
    }
}

```

---

<div class="post-metadata">

**Author:** ![system](https://geant4-forum.web.cern.ch/uploads/default/original/1X/ce519a43ebdf7ac48a98a03c6ad6eea013b68c46.png) [@system](https://geant4-forum.web.cern.ch/u/system)\
**Post date:** [February 21, 2024, 3:45pm UTC](https://geant4-forum.web.cern.ch/t/runtime-exception-during-execution-leading-to-a-crash/11768/4 "2024-02-21T15:45:26Z")

</div>

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.
