title: c# Parallel Programming, Parallel.for throwing null reference error in Epplus
body: <p>I am new to parallel programming.I have a requirement to Upload a data into database.Before uploading,it involve in three process,</p>
  
  <p>1.Data need to be validated.</p>
  
  <p>2.It need to be Converted into List.</p>
  
  <p>3.Finally it will be Converted in to another Type.i.e Mapping</p>
  
  <p>So for this process i used parallel programming.Validation has separate method and We have separate method for converting the excel in to list.This validation method is called from Converting method.This process should happened Concurrently.it works fine in normal for loop.But not in Parallel.for.For same file,at some case validation is successfully happened and at some case it shows error as</p>
  
  <blockquote>
    <p>Object reference not set to an instance of an object</p>
  </blockquote>
  
  <p>This error is coming only in validation method.Since data is available.</p>
  
  <p>Below i have the parallel code</p>
  
  <pre><code>Parallel.For(2, BelieversWorkSheet.Dimension.End.Row, new ParallelOptions { MaxDegreeOfParallelism = BelieversWorkSheet.Dimension.End.Row },rowIterator =&gt; 
  {
  var BelieverCSVs = new BelieversSignupUploadDetails();
  if (!string.IsNullOrWhiteSpace(BelieversWorkSheet.Cells[rowIterator, 1]?.Value?.ToString()) || !string.IsNullOrWhiteSpace(BelieversWorkSheet.Cells[rowIterator, 2]?.Value?.ToString()) || !string.IsNullOrWhiteSpace(BelieversWorkSheet.Cells[rowIterator, 3]?.Value?.ToString()) || !string.IsNullOrWhiteSpace(BelieversWorkSheet.Cells[rowIterator, 4]?.Value?.ToString()))
      {
      var SingeRowValidationSummary = RequiredValidationForARow(BelieversWorkSheet.Cells[rowIterator, 1, rowIterator, 4], rowIterator, BelieversWorkSheet);//Validating Method is called
      if (SingeRowValidationSummary.Count==0|| SingeRowValidationSummary==null)//add &amp;&amp; decimal.TryParse(BelieversWorkSheet.ToString(), out Stand) &amp;&amp; decimal.TryParse(E.ToString(), out Express)
      {
      BelieverCSVs.FirstName = BelieversWorkSheet.Cells[rowIterator, 1].Value.ToString();
      BelieverCSVs.MiddleName = BelieversWorkSheet.Cells[rowIterator, 2].Value.ToString();
      BelieverCSVs.LastName = BelieversWorkSheet.Cells[rowIterator, 3].Value.ToString();
      BelieverCSVs.DOB = DateTime.Parse(BelieversWorkSheet.Cells[rowIterator, 4].Value.ToString());
      Believers.Add(BelieverCSVs);
      }
      else
      {
      ValidationSummaryForSingle.AddRange(SingeRowValidationSummary);
      }
      }
      });
  </code></pre>
  
  <p>Below is the validation method where error occurred.</p>
  
  <pre><code> private List&lt;string&gt; RequiredValidationForARow(ExcelRangeBase CurrentRow,int rowstart,ExcelWorksheet IndexFind)
      {
  List&lt;string&gt; ValidationMessage=new List&lt;string&gt;();
  Regex regex = new Regex(@"^(?!\.)(""([^""\r\\]|\\[""\r\\])*""|"+ @"([-a-z0-9!#$%&amp;'*+/=?^_`{|}~]|(?&lt;!\.)\.)*)(?&lt;!\.)"+ @"@[a-z0-9][\w\.-]*[a-z0-9]\.[a-z][a-z\.]*[a-z]$");
  if (string.IsNullOrWhiteSpace(((object[,])CurrentRow?.Value)[0, 0]?.ToString()))
  ValidationMessage.Add( "First Name in row\t"+ IndexFind.Cells[rowstart, 1].ToString() +" is Missing" );
  if (string.IsNullOrWhiteSpace(((object[,])CurrentRow?.Value)[0, 2]?.ToString()))
  ValidationMessage.Add("Last Name in row\t"+ IndexFind.Cells[rowstart, 3].ToString()+" is Missing");
  if (string.IsNullOrWhiteSpace(((object[,])CurrentRow?.Value)[0, 5]?.ToString()))
  ValidationMessage.Add("Primary Email in row \t" + IndexFind.Cells[rowstart, 4].ToString() + " is Missing" );
  else
  if (!regex.IsMatch(((object[,])CurrentRow?.Value)[0, 4]?.ToString()))
  ValidationMessage.Add("Primary Email in row \t" + IndexFind.Cells[rowstart, 4].ToString() + " is invalid");
  return ValidationMessage;
  }
  </code></pre>
  
  <p>Note-this error is not coming all time.if i am tring to upload same file for 10 times with same data,it showing this error for 4 time</p>
  
  <p>Can any one help on this </p>
  
  <p>Thanks in advance</p>