Hans Rudel
Hans Rudel

Reputation: 3611

Unit testing with NUnit. Null object error

I keep getting a null object reference error when im running one of my unit test.

Unit Test:

    [Test]
    public void EnumeratedData_ValidInputType_NoErrorAdded()
    {
        List<String> errorMessageList = new List<string>();
        UserInputEntity myEntity = new UserInputEntity();

        myEntity.DataTypes = new List<string>();
        myEntity.DataTypes.Add("DateTime");
        myEntity.DataTypes.Add("datetime");
        myEntity.DataTypes.Add("decimal");
        myEntity.DataTypes.Add("decIMAL");
        myEntity.DataTypes.Add("DOUble");
        myEntity.DataTypes.Add("double");
        myEntity.DataTypes.Add("FLOat");
        myEntity.DataTypes.Add("float");
        myEntity.DataTypes.Add("INT");
        myEntity.DataTypes.Add("int");

        PathReader reader = new PathReader();
        IOManager manager = new IOManager(reader);
        VerificationManager testObject = new VerificationManager(manager);

        testObject.EnumeratedDataTypes(myEntity, errorMessageList);
        Assert.AreEqual(errorMessageList.Count, 0);
    }

Method Code:

    public void EnumeratedDataTypes(UserInputEntity inputs, List<String> errorMessageList)
    {
        inputs.EnumeratedDataTypes = new int[inputs.DataTypes.Count];
        try
        {
            for (int i = 0; i < inputs.DataTypes.Count; i++)
            {
                inputs.EnumeratedDataTypes[i] = (int)Enum.Parse(typeof(Enumerations.ColumnDataTypes), inputs.DataTypes[i].ToUpper());
            }
        }
        catch (Exception ex)
        {
            errorMessageList.Add(ex.Message);
        }
    }

Enum:

class Enumerations
{
    public enum ColumnDataTypes
    {
        DATETIME = 0,
        DECIMAL = 1,
        DOUBLE = 2,
        FLOAT = 3,
        INT = 4
    }
}

ErrorMessage:

FrazerMann.CsvImporter.Entity.Test.EntityVerificationTests.EnumeratedData_ValidInputType_NoErrorAdded: System.NullReferenceException : Object reference not set to an instance of an object.

Im assumin im overlooking something stupidly simple but i cant see it. id appreciate it if someone could put me out of my misery.

Upvotes: 0

Views: 1012

Answers (1)

Anders Gustafsson
Anders Gustafsson

Reputation: 15971

In your EnumeratedDataTypes method, you first set the length of the inputs.EnumeratedDataTypes property to inputs.ColumnNames.Count, which is equal to 0 since it has not been filled with data yet (it is only the List capacity that has been set to 9).

Next, when filling this array property with data you loop from 0 to index (including) inputs.DataTypes.Count:

for (int i = 0; i <= inputs.DataTypes.Count; i++)

I count the size of the input.DataTypes list to 10. Thus, you will try to write data to an empty array.

I propose the following changes:

First, initialize the inputs.EnumeratedDataTypes array as follows:

inputs.EnumeratedDataTypes = new int[inputs.DataTypes.Count];

Second, use < instead of <= in the for loop:

for (int i = 0; i < inputs.DataTypes.Count; i++)

Upvotes: 1

Related Questions