即使列表项不为null,也为NullReference异常

时间:2015-02-07 02:37:43

标签: c# asp.net-mvc

我必须在这里遗漏一些东西,但我似乎有一个List,其项目被null捕获,即使我已经检查并确认它们没有。

我的ASP.NET MVC项目的单元测试是在NullReferenceException循环上抛出foreach,但我找不到任何理由,所以我把一些支票扔进了我的码。令我惊讶的是,check语句没有捕获任何空值,但异常仍然存在。这是相关的代码:

[HttpPost]
[ValidateAntiForgeryToken]
public PartialViewResult CreateSimilar(int rebateId, List<AddressInput> addresses, bool recdResults = false)
{
    List<RebateHeader> newRebates = new List<RebateHeader>();
    RebateHeader entity = null;

    int newId = -1;

    if (!recdResults)
    {
        var repo = _db as HeaderRepository;
        List<PotentialDuplicate> allDups = new List<PotentialDuplicate>();

        //A few checks for null objects to illustrate my point-------
        if (addresses == null)
            throw new ApplicationException("Addresses was null"); //Not thrown
        else
            System.Console.WriteLine("Addresses was not null"); //This line is hit

        foreach (AddressInput address in addresses)
        {
            if (address == null)
                throw new ApplicationException("Address was null"); //Not thrown
            else
                System.Console.WriteLine("Address was not null"); //This line is hit
        }

        var test = addresses[0];
        System.Console.WriteLine(test.City); //This line returns a value
        System.Console.WriteLine(test.State); //This line returns a value
        //End checks---------------------------------------------------

        foreach (AddressInput address in addresses) //NullReferenceException THROWN HERE
        {
            List<PotentialDuplicate> dups = repo.GetDuplicateAddresses(
                address.Address, address.City, address.State).ToList();
            if (dups.Count > 0)
            {
                allDups.AddRange(dups);
            }
        }

        if (allDups.Count > 0)
        {
            return PartialView("_AddressDialogPotentialDup", allDups);
        }
    }
    . . . //Additional code truncated
    return PartialView("_IndexNoPager", model);
}

我必须在这里遗漏一些东西,但我没有看到它。有任何想法吗?

为了进一步参考,这里是失败的单元测试:

[Test]
public void CreateSimilar_Adds_1_New_Record()
{
    EntryController controller = new EntryController(repository);
    List<AddressInput> addresses = new List<AddressInput> 
    {   
        new AddressInput 
    { 
        Address = "Duplicate St.", City = "Testville", State = "MN", 
        ClosingDate = null, Quarter = "115" 
    }
    };

    controller.CreateSimilar(1, addresses); //Unit test FAILS HERE

    Assert.AreEqual(4, repository.GetAll().Count());
    Assert.AreEqual(1, repository.Added.Count);
    Assert.AreEqual("Test Duplicate 1", repository.Added[0].Address);
}

更新:在回复下面的评论时,这是GetDuplicateAddresses的代码:

public IEnumerable<PotentialDuplicate> GetDuplicateAddresses(
    string address, string city, string state)
{
    var result = new List<PotentialDuplicate>();

    using (SqlCommand cmd = new SqlCommand("dbo.GetDuplicateAddresses", (SqlConnection)this.Database.Connection))
    {
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@Address", address);
        cmd.Parameters.AddWithValue("@City", city);
        cmd.Parameters.AddWithValue("@State", state);

        cmd.Connection.Open();
        using (var reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
        {
            while (reader.Read())
            {
                result.Add(new PotentialDuplicate
                {
                    OrigAddress = address,
                    RebateIdMatch = reader.GetInt32(0),
                    Address = reader.GetString(1),
                    MatchType = reader.GetString(2)
                });
            }

            return result;
        }
    }
}

这是我在单元测试中使用的存根:

public IEnumerable<PotentialDuplicate> GetDuplicateAddresses(
    string address, string city, string state)
{
    var result = new List<PotentialDuplicate>();
    return result;
}

2 个答案:

答案 0 :(得分:1)

我会查看是否&#34; dups&#34;在执行dups.Count()&gt;之前为null 0

答案 1 :(得分:0)

事实证明,问题在于这个演员:

var repo = _db as HeaderRepository;

我不得不重构其他一些代码,如果以后出现类似问题,我会稍微更改此行以引发InvalidCastException

var repo = (IHeaderRepository)_db;

我甚至没想过要检查这个,因为编译器突出显示foreach循环作为异常的位置。感谢Tim Southard和thepirat000指出循环中的代码块可能导致编译器引发异常。