-2

My problem is about an object not set to an instance after the first line inside the foreach loop. I really had a hard time thinking what should be instantiated here. So please guys, i just want to map the data from the DataTable to the a new List.

It errors out after this line,

aa.fieldParams[counter].field = row["ParameterField"].ToString();

Here is my actual code.

    public class ParsedData
{
    public static void ParseData(DataTable parsedData)
    {
        ObjectProperties aa = new ObjectProperties();

        int counter = 0;
        foreach (DataRow row in parsedData.Rows)
        {
            //parsedData.Rows = new DataTable[parsedData.Rows.Count];
            aa.fieldParams[counter].field = row["ParameterField"].ToString();
            aa.fieldParams[counter].parameterType = row["ParameterType"].ToString();
            aa.fieldParams[counter].length = Convert.ToInt32(row["ParameterLength"]);
            aa.fieldParams[counter].setIteration = Convert.ToInt32(row["NumberOfIterations"].ToString());
            counter++;
        }
    }
}

Here is the ObjectProperties class.

public class ObjectProperties
{
    public FieldParameters[] fieldParams { get; set; }

    public int Counter { get; set; }
}

public class FieldParameters
{
    public string field { get; set; }
    public int length { get; set; }
    public int setIteration { get; set; }
    public string parameterType { get; set; }
}

aa.fieldParams[] do not have any values yet because the values of this will be coming from the parsedData.Rows. I am currently having the correct values on row but after the first line inside the loop it errors out.

parsedData values are coming from CSV file.

vonndutch
  • 43
  • 1
  • 2
  • 7

3 Answers3

1

Initialize your array first:

ObjectProperties aa = new ObjectProperties();
aa.fieldParams = new FieldParameters[parsedData.Rows.Count];    //initialize your array first

You also haven't initialized your Array's elements:

foreach (DataRow row in parsedData.Rows)
{
    aa.fieldParams[counter] = new YourArrayType();  //instantiate the current element
    //...your code...
    counter++;
}

Remember you have to instantiate every element of the array when your array contains reference type elements.

Shaharyar
  • 12,254
  • 4
  • 46
  • 66
  • Doing this inside the foreach loop would be incorrect, here fieldParams would be an array not the fieldParams[counter]. Otherwise your point is correct this would be most probable reason for "Object reference not set" – Mrinal Kamboj May 20 '15 at 05:18
  • I am not initializing the entire array, but it's objects. If array is not initialized, there must has been the compile time error. But there's a run time exception, it means they array is initialized but not it's elements. – Shaharyar May 20 '15 at 05:20
  • Uninitalized array will not throw compile time error, it is a run time error and here the elements of the array seems to be string, which anyway doesn't need explicit initialization – Mrinal Kamboj May 20 '15 at 05:32
  • No, you've to initialize every element of array while using reference type elements. You need to check a bit of it :) – Shaharyar May 20 '15 at 05:39
  • This works! life saver! Thanks! – vonndutch May 20 '15 at 06:19
  • @Shaharyar correct, but as I suggested array or array elements initialization will all be run time issues, not compile time. In this case I misunderstood the array to be a string[], where each element doesn't need explicit initialization, as it behaves like a value type. I have modified my code answer to reflect the correct response – Mrinal Kamboj May 20 '15 at 09:27
  • @MrinalKamboj Regarding error, yes you're right. It'll be a runtime exception – Shaharyar May 20 '15 at 09:31
0

Check for null like this before using .ToString() on DataRow.

if (!(row["ParameterField"] is DBNull))
  aa.fieldParams[counter].field = row["ParameterField"].ToString();

Also check for null values on other lines before casting.

murtazat
  • 399
  • 3
  • 12
0

You are using aa.fieldParams but I only see aa declared and initialized. What about fieldParams? Do you need to initialize it? Can you post the code for ObjectProperties class to see what does fieldParams hold?

May be aa.fieldParams is null[counter]?

Also if you do not trust the Column names in the DataRow then better check if the column with that name exists in the datatable before trying to access it.

Thanks,