0

So I am trying to create an array of structures for exporting data from a database to a formatted file for later import to another database and/or reports.

I know what a Null Reference Exception is, based upon the reading I had done in the link I provided in my comments below, I believed that I had hit an edge case that I needed clarification on, only to learn the NEW keyword while making me an array like I wanted, didn't initiate each class within the array an assumption I made about .NET which of course was faulty. While Yes this question has to do with a Null Reference Exception I'm not asking what a Null Reference Exception is.

This is My struct

public class udtExportType
{
    public int intPatientID = 0;
    public string strFirstName = string.Empty;
    public string strMiddleName = string.Empty;
    public string strLastName = string.Empty;
}

Here is where I initialize the structure

// Create an array of structures based upon the length of the Patient Data Array

udtExportData = new udtExportType[intRecordCount];
//A Professional Masseuse used to massage data from the VPatientExportData View.
PatientDataMasseuse(strPatientData, ref udtExportData);

Here is where I try adding the Data from the PatientDataMasseuse

// Add To Structure
udtExportdata[intIndex].strFirstName = strPatientData[intIndex].Substring(intIDIndex, (intFullNameIndex - intIDIndex));

but on the above line I get a Null reference exception, how is this possible? I used the new keyword when I initialize and I passed it by ref. I even tried inserting data before it went into the PatientDataMasseuse procedure as a result, and I got the same error. I read some questions on this site, and hunted around MSDN and I see no difference between what I have typed versus anyone else.

Help is much appreciated.

* EDIT *

I assumed .NET would do this for me with the new keyword but it turns out you have to do it manually for each element in the array

            for(intIndex = 0; intIndex < Length(); intIndex += 1)
            {
                udtExportData[intIndex] = new udtExportType();
            }
Adam Schneider
  • 275
  • 1
  • 5
  • 18
  • [SO : What is a NullRef and how do i fix it](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Kalten Jan 10 '17 at 22:22
  • 3
    Technically that is an array of _classes_ which are refrence types, so each value in `udtExportType[]` is null unless you set it to an instance. – D Stanley Jan 10 '17 at 22:23
  • 1
    Also, Hungarian notation is typically not used in C# code – BradleyDotNET Jan 10 '17 at 22:23
  • what is `strPatientData[intIndex]`? – McNets Jan 10 '17 at 22:24
  • Either udtExportdata[intIndex] or strPatientData[intIndex] is null. Which one, well use debugger to find out. – Ivan Ičin Jan 10 '17 at 22:26
  • Thank you Stanley, I initialized each reference in the Array separately something I assumed .NET would just do for me when I used the NEW Keyword. lol. – Adam Schneider Jan 10 '17 at 22:30
  • I hardly think this is a Duplicate question, while technically yes does deal with a Null reference, I'm not asking what a Null reference is, I know what it is, I merely made an assumption with .NET that really anyone else could have made. Upon reading other questions and resources online [link](http://www.dreamincode.net/forums/topic/95520-coding-an-array-of-structure/), [link](http://stackoverflow.com/questions/18290766/vb-program-array-of-structure-and-class), [link](http://stackoverflow.com/questions/18772718/array-of-structs-example). Led me to believe I had hit some edge case. – Adam Schneider Jan 10 '17 at 22:32
  • 1
    @TurdBurgler The linked post has almost your *exact* code under 'Array Elements'. You may know what a null reference exception is, but the second part of the title is 'and how do I fix it?' – Rob Jan 10 '17 at 22:44

0 Answers0