尝试写入空小数值时,Filehelpers NullReferenceException

时间:2022-03-07 16:54:25

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

使用FileHelpers库时,我在尝试编写.csv文件时收到NullReferenceException。

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;

   }
}

2 个解决方案

#1


1  

You can use a custom converter.

您可以使用自定义转换器。

public class NullableDecimalConverter : FileHelpers.ConverterBase
{
    public override object StringToField(string from)
    {
        return from;
    }

    public override string FieldToString(object fieldValue)
    {
        if (fieldValue == null)
            return String.Empty;
        return fieldValue.ToString();
    }
}

You need to modify your record class to add a [FieldConverter()] attribute to any decimal? field.

您需要修改记录类以将[FieldConverter()]属性添加到任何小数?领域。

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

    [FileHelpers.FieldConverter(typeof(NullableDecimalConverter))]
    public decimal? mydecimal;

}

#2


0  

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.

讨厌回答我自己的问题,但FileHelpers 2.9.9解决了这个问题。它曾经在官方网站上提供(标记为测试版),但现在无法找到它。

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

但是在NuGet中可以使用名为FileHelpers-stable的软件包

#1


1  

You can use a custom converter.

您可以使用自定义转换器。

public class NullableDecimalConverter : FileHelpers.ConverterBase
{
    public override object StringToField(string from)
    {
        return from;
    }

    public override string FieldToString(object fieldValue)
    {
        if (fieldValue == null)
            return String.Empty;
        return fieldValue.ToString();
    }
}

You need to modify your record class to add a [FieldConverter()] attribute to any decimal? field.

您需要修改记录类以将[FieldConverter()]属性添加到任何小数?领域。

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

    [FileHelpers.FieldConverter(typeof(NullableDecimalConverter))]
    public decimal? mydecimal;

}

#2


0  

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.

讨厌回答我自己的问题,但FileHelpers 2.9.9解决了这个问题。它曾经在官方网站上提供(标记为测试版),但现在无法找到它。

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

但是在NuGet中可以使用名为FileHelpers-stable的软件包