值不能为空。参数名称:在项目中使用通用服务时的源 [英] Value cannot be null. Parameter name: source when using generic service in project

查看:103
本文介绍了值不能为空。参数名称:在项目中使用通用服务时的源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在主项目中为FindUser使用此代码:



i using this code for `FindUser` in Main Project :

public MainWindow()
    {
        var usersManagerService = StructureMapDefnation.Container.GetInstance<IUser>();
        InitializeComponent();
        usersManagerService.Find(x => x.Username == "k" && x.Password == "k");
    }





i调试项目,转到服务层并使用查找功能:





i Debug the Project , it Go to Service Layer and using the Find Function :

public T Find(Func<T, bool> predicate)
    {
        return Entites.Where(predicate).FirstOrDefault();
    }





但它显示我的错误:





but it show me this error :

Value cannot be null. Parameter name: source





是什么问题?我怎么能解决这个问题呢?



我尝试过:





whats the problem ? how can i solve this problem ?

What I have tried:

public T Find(Func<T, bool> predicate)
    {
        return Entites.Where(predicate).FirstOrDefault();
    }

推荐答案

您是否在抛出错误的行上设置了BreakPoint,运行代码,然后检查值在执行该行之前?您将看到什么是null并抛出错误。



如果您不熟悉使用BreakPoints,此视频将有所帮助:使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]
Have you set a BreakPoint on the line that throws the error, run the code, then check the values before the line is executed? You will see what is null and is throwing the error.

If you are unfamiliar with working with BreakPoints, this video will help: Basic Debugging with Visual Studio 2010 - YouTube[^]


我们无法为您解决此问题 - 这取决于您的数据,我们没有。

可能,您的 Entites 集合为空,这就是错误的原因 - 您无法迭代空集合。

所以,它取决于你。

查找函数的第一行放置断点,并通过调试器运行代码。然后查看您的代码,并查看您的数据并找出手动应该发生的事情。然后单步执行每一行检查您预期发生的情况正是如此。如果不是,那就是当你遇到问题时,你可以回溯(或者再次运行并仔细观察)以找出原因。


对不起,但我们不能为你做到这一点 - 时间让你学习一门新的(非常非常有用的)技能:调试!
We can't fix this for you - it depends on your data, which we don't have.
Probably, your Entites collection is null and that's why the error - you can't iterate a null collection.
So, its going to be up to you.
Put a breakpoint on the first line in the Find function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!


找到答案。我评论过这一行:
find the answer . i commented this line :
Entites = Uow.Set<T>();


这篇关于值不能为空。参数名称:在项目中使用通用服务时的源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆