Search code examples
c#.netnullreferenceexceptionfilehelpers

Filehelpers NullReferenceException when trying to write a null decimal value


When using the FileHelpers library I am getting a NullReferenceException when trying to write a .csv file.

I have narrowed the problem down. Whenever I have a null decimal? it throws this exception. It works fine on reading, just not writing.

I have included a sample that shows the same problem as my app:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication11
{
   class Program
   {
      static void Main(string[] args) {
         rec record = new rec { id = 1, mydecimal = null };
         List<rec> records = new List<rec> { record };

         FileHelpers.FileHelperEngine<rec> engine = new FileHelpers.FileHelperEngine<rec>();

         Console.WriteLine(engine.WriteString(records));

      }
   }

   [FileHelpers.DelimitedRecord(",")]
   public class rec
   {
      public int id;
      public decimal? mydecimal;

   }
}

Solution

  • Hate to answer my own question, but FileHelpers 2.9.9 fixes this problem. It used to be available on the official site (marked as beta), but can't find it now.

    It is however available in NuGet under a package called FileHelpers-stable