diff --git a/Modules/Learning/Supervised/include/otbNeuralNetworkMachineLearningModel.txx b/Modules/Learning/Supervised/include/otbNeuralNetworkMachineLearningModel.txx
index 87eed252e00649ba21cd6c98e8c44441b99ef678..b76e6dd70d3f57741ac83ef9ce448362d9e942fc 100644
--- a/Modules/Learning/Supervised/include/otbNeuralNetworkMachineLearningModel.txx
+++ b/Modules/Learning/Supervised/include/otbNeuralNetworkMachineLearningModel.txx
@@ -73,10 +73,14 @@ template<class TInputValue, class TOutputValue>
 void NeuralNetworkMachineLearningModel<TInputValue, TOutputValue>::LabelsToMat(const TargetListSampleType * labels,
                                                                                cv::Mat & output)
 {
-  unsigned int nbSamples = labels->Size();
+  unsigned int nbSamples = 0;
+  if (labels != NULL)
+    {
+    nbSamples = labels->Size();
+    }
 
   // Check for valid listSample
-  if (labels != NULL && nbSamples > 0)
+  if (nbSamples > 0)
     {
     // Build an iterator
     typename TargetListSampleType::ConstIterator labelSampleIt = labels->Begin();