2011-11-08 2 views
2

FileHelpers 라이브러리를 사용할 때 .csv 파일을 쓰려고 할 때 NullReferenceException이 발생합니다.Filehelpers null 십진수 값을 쓰려고 할 때 NullReferenceException

나는 문제를 줄였습니다. 내가 십진수를 가질 때마다? 이 예외를 throw합니다. 독서에서는 잘 작동하지만 쓰지는 않습니다.

내 응용 프로그램과 같은 문제를 보여주는 샘플 포함했다 :

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; 

    } 
} 
+0

NULL이 는 아무 문제가 없을 것이라는 패키지에서 NuGet에서 그러나 사용할 수 있습니다. http://www.filehelpers.com/example_nullable.html –

+0

내가 사용하는 버전은 2.0.0.0이며 nuget에서 제공됩니다. 이것은 최신 버전 인 것 같습니다. 위의 코드는 예외를 throw합니다. 전체 프로젝트를 github에 올렸지 만 vs2010 솔루션/프로젝트에 래핑 된 내용은 다음과 같습니다. https://github.com/estubbs/Stack-overflow-fh-questions – Erick

답변

0

내 자신의 질문에 싫어하지만 FileHelpers 2.9.9는이 문제를 해결합니다. 이전에 공식 사이트 (베타로 표시된)에서 사용할 수 있었지만 지금은 찾을 수 없습니다.

그것은 당신이 FileHelpers의 고대 버전에 있지 않는 FileHelpers 안정

1

당신은 사용자 정의 변환기를 사용 할 수 있습니다.

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(); 
    } 
} 

당신은 어떤 decimal? 필드에 [FieldConverter()] 속성을 추가하여 기록 클래스를 수정해야합니다.

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

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

} 
관련 문제