0

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.

1 Answer 1

1

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++)
Sign up to request clarification or add additional context in comments.

5 Comments

@Andres you beat me to the answer. Just one clarification, the count is actually 0 as the list has not been used yet.
@JTMon You are right, sorry! It is of course the List capacity that is being set in the constructor, not the predefined size. Will update my response accordingly.
Anders, cheers for replying. Good point about the <= and initializing. Unfortunately its still saying i have an error of the same type. Ill edit my original question with he updated code. +1 though :)
Do you have more information as to exactly where the exception is thrown? It is not obvious that any part of your code should throw NullReferenceException. Could it be that any of the constructors you are calling throws? I am thinking of the reader, manager and testObject initializers.
Weird, i just reverted back to a previous version, made those changes u suggested and then re-ran the tests and it works. Thanks so much for your help, i really appreciate it!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.