5

I'm using version 2.0 of the FileHelpers library which is documented as being able to handle .NET 2.0 Nullable types.

I'm using the code given in the example from the documentation:

[DelimitedRecord("|")]   
public class Orders   
{   
    public int OrderID;   

    public DateTime? OrderDate;   

    [FieldConverter(ConverterKind.Date, "ddMMyyyy")]     
    public DateTime? RequiredDate;   

    public int? ShipVia;   
}   

With a FileHelperEngine I can successfully read in a file which has no value for the OrderDate, RequiredDate or ShipVia fields. The file looks like:

1|||

However, I cannot then write out the resulting Orders[] to file - the library throws a NullReferenceException, stack trace below:

at FileHelpers.ConvertHelpers.CultureConverter.FieldToString(Object from) at FileHelpers.FieldBase.BaseFieldString(Object fieldValue) at FileHelpers.DelimitedField.CreateFieldString(StringBuilder sb, Object fieldValue) at FileHelpers.FieldBase.AssignToString(StringBuilder sb, Object fieldValue) at FileHelpers.RecordInfo.RecordToString(Object record) at FileHelpers.FileHelperEngine1.WriteStream(TextWriter writer, IEnumerable1 records, Int32 maxRecords) at FileHelpers.FileHelperEngine1.WriteFile(String fileName, IEnumerable1 records, Int32 maxRecords) at FileHelpers.FileHelperEngine1.WriteFile(String fileName, IEnumerable1 records) at TestingFileHelpers.Program.Main(String[] args) in C:\dev\src\TestingFileHelpers\TestingFileHelpers\Program.cs:line 19 at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ThreadHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart()

I'm sure I must be missing something here but I cannot figure out what it is. Any help much appreciated.

skaffman
  • 398,947
  • 96
  • 818
  • 769
nowhere_fast
  • 53
  • 1
  • 4

2 Answers2

3

Can you try with the last version of the library:

http://teamcity.codebetter.com/viewLog.html?buildId=21768&tab=artifacts&buildTypeId=bt65 (login as guest)

If that version dot fix the error just tell me in a comment and I will add a test case to the lib to ensure it works

Marcos Meli
  • 3,468
  • 24
  • 29
  • 1
    Yes, seems to be fixed in that version. Thanks! – nowhere_fast Feb 23 '11 at 10:01
  • I was having the exact same problem, but on a nullable decimal. I pulled down the latest build from the above link and it also fixed me up. Can I recommened @nowhere_fast that you update the web page to reference this new build? It would have saved me a bunch of time! Luckily, StackOverflow came through (like always) with an answer. – codechurn Sep 17 '12 at 18:08
  • @MarcosMeli can you update your sourcefourge site to reference the builds available on TeamCity? I looked there initially and could not find a link -- luckily, I found my old post here. – codechurn Dec 05 '12 at 17:48
0

Use the [FieldNullValue("")] attribute to specify default values for the empty fields.

edosoft
  • 17,121
  • 25
  • 77
  • 111
  • 2
    This attribute can't be added to a field of type int? - it results in a BadUsageException being raised when instantiating the FileHelperEngine, as the value provided ("") is not a nullable int. I don't think this attribute is the answer as it is used to assign a "magic value" of the given type when no value is provided in the file i.e. this is pre-nullable types. – nowhere_fast Feb 22 '11 at 12:40