From b05fa72a84e3bda9029a5e8229570e12a94b027e Mon Sep 17 00:00:00 2001
From: Matt King <matthew.king@stfc.ac.uk>
Date: Fri, 23 Oct 2015 10:39:03 +0100
Subject: [PATCH] Added null check when fetching parameters from IDF

Recent build failures happened because some of the components
being fetched did not exist for some IDFs. resulting in
a null pointer being returned. a null check has been implmeneted
so that if the component pointer in null then we just set the
value in slitCalculator to zero.

Refs #13758
---
 .../MantidQtMantidWidgets/SlitCalculator.h    |  6 ++--
 MantidQt/MantidWidgets/src/SlitCalculator.cpp | 30 +++++++++++++++----
 2 files changed, 27 insertions(+), 9 deletions(-)

diff --git a/MantidQt/MantidWidgets/inc/MantidQtMantidWidgets/SlitCalculator.h b/MantidQt/MantidWidgets/inc/MantidQtMantidWidgets/SlitCalculator.h
index 633b24f638a..00d733c0027 100644
--- a/MantidQt/MantidWidgets/inc/MantidQtMantidWidgets/SlitCalculator.h
+++ b/MantidQt/MantidWidgets/inc/MantidQtMantidWidgets/SlitCalculator.h
@@ -40,10 +40,7 @@ class EXPORT_OPT_MANTIDQT_MANTIDWIDGETS SlitCalculator : public QDialog {
 public:
   SlitCalculator(QWidget *parent);
   virtual ~SlitCalculator();
-  void setInstrument(std::string instrumentName);
-  Mantid::Geometry::Instrument_const_sptr getInstrument();
   void setCurrentInstrumentName(std::string instrumentName);
-  std::string getCurrentInstrumentName();
   void processInstrumentHasBeenChanged();
 
 protected:
@@ -54,6 +51,9 @@ private:
   std::string currentInstrumentName;
   void setupSlitCalculatorWithInstrumentValues(
       Mantid::Geometry::Instrument_const_sptr);
+  std::string getCurrentInstrumentName();
+  Mantid::Geometry::Instrument_const_sptr getInstrument();
+  void setInstrument(std::string instrumentName);
 private slots:
   void on_recalculate_triggered();
 };
diff --git a/MantidQt/MantidWidgets/src/SlitCalculator.cpp b/MantidQt/MantidWidgets/src/SlitCalculator.cpp
index 8ef076bf8cb..68585e2b264 100644
--- a/MantidQt/MantidWidgets/src/SlitCalculator.cpp
+++ b/MantidQt/MantidWidgets/src/SlitCalculator.cpp
@@ -11,12 +11,16 @@ SlitCalculator::SlitCalculator(QWidget *parent) {
   Q_UNUSED(parent);
   ui.setupUi(this);
   if (currentInstrumentName == "") {
+    // set up the initial instrument if there is not one associated
+    // with slit calculator
     currentInstrumentName = "INTER";
     setInstrument(currentInstrumentName);
   }
   on_recalculate_triggered();
 }
 void SlitCalculator::processInstrumentHasBeenChanged() {
+  // used in refl main window to indicate that slitCalculator fields
+  // need to update because another instrument has been selected.
   on_recalculate_triggered();
 }
 SlitCalculator::~SlitCalculator() {}
@@ -41,7 +45,7 @@ void SlitCalculator::setInstrument(std::string instrumentName) {
     this->instrument = Mantid::API::InstrumentDataService::Instance().retrieve(
         instrumentNameMangled);
   } else {
-    // We set the XML that we have found for the instrument.
+    // We set the instrument from XML that we have found.
     Mantid::API::Progress *prog = new Mantid::API::Progress();
     this->instrument = parser.parseXML(prog);
     delete prog;
@@ -51,14 +55,28 @@ void SlitCalculator::setInstrument(std::string instrumentName) {
 
 void SlitCalculator::setupSlitCalculatorWithInstrumentValues(
     Mantid::Geometry::Instrument_const_sptr instrument) {
+  // fetch the components that we need for values from IDF
   auto slit1Component = instrument->getComponentByName("slit1");
   auto slit2Component = instrument->getComponentByName("slit2");
   auto sampleComponent = instrument->getComponentByName("some-surface-holder");
-  // convert between metres and millimetres
-  const double s1s2 = 1e3 * slit1Component->getDistance(*slit2Component);
-  ui.spinSlit1Slit2->setValue(s1s2);
-  const double s2sa = 1e3 * slit2Component->getDistance(*sampleComponent);
-  ui.spinSlit2Sample->setValue(s2sa);
+  // check that they have been fetched from the IDF
+  if (slit1Component.get() != NULL && slit2Component.get() != NULL &&
+      sampleComponent.get() != NULL) {
+    // convert from meters to millimeters
+    const double s1s2 = 1e3 * (slit1Component->getDistance(*slit2Component));
+    // set value in field of slitCalculator
+    ui.spinSlit1Slit2->setValue(s1s2);
+    // convert from meters to millimeters
+    const double s2sa = 1e3 * (slit2Component->getDistance(*sampleComponent));
+    // set value in field of slitCalculator
+    ui.spinSlit2Sample->setValue(s2sa);
+  } else {
+    // the parameters slit1, slit2 and sample-holder where not found
+    // set the values in SlitCalculator up so that it is obvious
+    // we did not retrieve them from any IDF.
+    ui.spinSlit1Slit2->setValue(0.0);
+    ui.spinSlit2Sample->setValue(0.0);
+  }
 }
 Mantid::Geometry::Instrument_const_sptr SlitCalculator::getInstrument() {
   return instrument;
-- 
GitLab