Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何解析值不能为null。参数名称:linq中的源?_C#_Linq - Fatal编程技术网

C# 如何解析值不能为null。参数名称:linq中的源?

C# 如何解析值不能为null。参数名称:linq中的源?,c#,linq,C#,Linq,我不知道我为什么会犯这种错误。这种情况有时会发生,我怀疑我的代码在关闭应用程序时仍然有线程在运行。所以当我再次打开它时,它发生了 Value cannot be null. Parameter name: source StackTree :    at System.Linq.Enumerable.Where[TSource](IEnumerable`1 source, Func`2 predicate)    at Susenas2015.ViewModels.Kuesioner.VMVse

我不知道我为什么会犯这种错误。这种情况有时会发生,我怀疑我的代码在关闭应用程序时仍然有线程在运行。所以当我再次打开它时,它发生了

Value cannot be null.
Parameter name: source
StackTree :
   at System.Linq.Enumerable.Where[TSource](IEnumerable`1 source, Func`2 predicate)
   at Susenas2015.ViewModels.Kuesioner.VMVsen15_KVal.SettingValidationAndRange(List`1 listTextBox, List`1 listCheckBox, TabControl tabControl) in d:\handita\Office\Project\Susenas 2015\Aplikasi Template Survei\Susenas2015\ViewModels\Kuesioner\VMVsen15_KVal.cs:line 430
   at Susenas2015.ViewModels.Kuesioner.VMVsen15_KVal.vSen15_K_Loaded(Object sender, RoutedEventArgs e) in d:\handita\Office\Project\Susenas 2015\Aplikasi Template Survei\Susenas2015\ViewModels\Kuesioner\VMVsen15_KVal.cs:line 846
   at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
   at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
   at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
   at System.Windows.UIElement.RaiseEvent(RoutedEventArgs e)
   at System.Windows.BroadcastEventHelper.BroadcastEvent(DependencyObject root, RoutedEvent routedEvent)
   at System.Windows.BroadcastEventHelper.BroadcastLoadedEvent(Object root)
   at MS.Internal.LoadedOrUnloadedOperation.DoWork()
   at System.Windows.Media.MediaContext.FireLoadedPendingCallbacks()
   at System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks()
   at System.Windows.Media.MediaContext.RenderMessageHandlerCore(Object resizedCompositionTarget)
   at System.Windows.Media.MediaContext.RenderMessageHandler(Object resizedCompositionTarget)
   at System.Windows.Media.MediaContext.Resize(ICompositionTarget resizedCompositionTarget)
   at System.Windows.Interop.HwndTarget.OnResize()
   at System.Windows.Interop.HwndTarget.HandleMessage(WindowMessage msg, IntPtr wpa`
这是我的密码

    private void SettingValidationAndRange(List<TextBox> listTextBox, List<CheckBox> listCheckBox, TabControl tabControl)
        {

            List<string> listNotDeclare = new List<string>();

            foreach (var textB in listTextBox)
            {
                if (textB.Tag != null)
                    break;

                Metadata metadata = ListMetadataKor.Where(
                    x => "text" + x.Field == textB.Name // this line 430

                ).FirstOrDefault();

                if (metadata == null)
                {
                    if (!string.IsNullOrEmpty(textB.Name))
                        listNotDeclare.Add(textB.Name);
                }
                else
                {
                    metadata.TabControl = tabControl;
                    textB.Tag = metadata;
                }

                textB.AddEvents();
                textB.AutomateFocus();

            }

            if (listNotDeclare.Count > 0)
            {
                Clipboard.SetText(string.Join(",", listNotDeclare.ToArray()));
                Dialog.Info("Ada beberapa Metadata tidak ditemukan data sudah dicopy ke clipboard");
            }

        }
然后我通过类的构造函数抛出变量

 public VMVsen15_KVal(
        MasterRT masterRT,
        VSEN15_K vSen15_K,
        IDaftarSusenas vmDaftarRTSusenas,
        List<Konsistensi> listKonsistensiKor,
        List<Metadata> listMetadataKor

        )
    {

        ListArtDetail = new ObservableCollection<ARTDetailVal>();

        this.ListKonsistensiKor = listKonsistensiKor;
        this.ListMetadataKor = listMetadataKor;
公共VMVsen15_KVal(
马斯特,
VSEN15_K VSEN15_K,
Idaftarusenas vmDaftarRTSusenas,
列出listKonsistensiKor,
列表列表Metadatakor
)
{
ListArtDetail=新的ObservableCollection();
this.ListKonsistensiKor=ListKonsistensiKor;
this.ListMetadataKor=ListMetadataKor;
我的工具是这样的

public static List<Konsistensi> GetKonsistensi(DataTable dataTable)
{
    List<Konsistensi> listMetadata = new List<Konsistensi>();
    for (int i = 0; i < dataTable.Rows.Count; i++)
    {
        Konsistensi k = new Konsistensi();
        object[] required = new object[] { DBNull.Value, "" };
        k.Field = dataTable.Rows[i][FIELD].ToString();
        if (string.IsNullOrWhiteSpace(k.Field)) continue;
        k.Message = dataTable.Rows[i][MESSAGE].ToString();
        var obj = dataTable.Rows[i][ORDER];
        k.Order = !required.Contains(dataTable.Rows[i][ORDER]) ? Convert.ToInt32(dataTable.Rows[i][ORDER]) : (int?)null;
        k.Page = !required.Contains(dataTable.Rows[i][PAGE]) ? Convert.ToInt32(dataTable.Rows[i][PAGE]) : (int?)null;
        k.Perlakuan = dataTable.Rows[i][PERLAKUAN].ToString();
        k.RelFields = dataTable.Rows[i][RELFIELDS].ToString();
        k.Rule = dataTable.Rows[i][RULE].ToString();

        if (dataTable.Rows[i][LEVEL].ToString().ToUpper() == ("ART"))
            k.LevelKonsistensi = LevelKonsistensi.ART;
        else if (dataTable.Rows[i][LEVEL].ToString().ToUpper() == ("RT"))
            k.LevelKonsistensi = LevelKonsistensi.RT;
        else if (dataTable.Rows[i][LEVEL].ToString().ToUpper() == ("RTWARNING"))
            k.LevelKonsistensi = LevelKonsistensi.RTWarning;
        else if (dataTable.Rows[i][LEVEL].ToString().ToUpper().Contains("ARTWARNING"))
            k.LevelKonsistensi = LevelKonsistensi.ARTWarning;
        else
            k.LevelKonsistensi = LevelKonsistensi.Lain;

        //k.LevelKonsistensi = dataTable.Rows[i][LEVEL].ToString().Contains("ART") ? LevelKonsistensi.ART : LevelKonsistensi.RT;
        if (k.IsEmpty())
            continue;

        listMetadata.Add(k);
    }
    return listMetadata;
}
公共静态列表GetKonsistensi(数据表)
{
List listMetadata=新列表();
for(int i=0;i
当您调用这样的Linq语句时:

// x = new List<string>();
var count = x.Count(s => s.StartsWith("x"));

因此,上面出现的错误是因为第一个参数,
source
(在上面的示例中是
x
)为空。

错误消息明确指出
source
参数为
null
。source是您正在枚举的可枚举项。在您的情况下,它是
ListMetadataKor
对象。在您第二次筛选它时,它肯定是
null
。请确保从未将
null
分配给此列表。只需检查代码中对此列表的所有引用,并查找分配

值不能为null。 参数名称:源

在查询空集合时出现上述错误

对于下面的演示,代码将导致此类异常

Console.WriteLine("Hello World");
IEnumerable<int> list = null;
list.Where(d => d ==4).FirstOrDefault();
Console.WriteLine(“你好世界”);
IEnumerable list=null;
其中(d=>d==4).FirstOrDefault();
下面是上述代码的输出

你好,世界 运行时异常(第11行):值不能为null。 参数名称:源

堆栈跟踪:

[System.ArgumentNullException:值不能为null。 Program.Main()处的参数名称:source]:第11行

在您的情况下,
ListMetadataKor
为空。 如果你想到处玩的话,那就是小提琴

System.ArgumentNullException:值不能为null。参数名称: 价值观

此错误消息没有多大帮助

您可以通过多种不同的方式获得此错误。错误可能并不总是与参数名:value有关。它可能是传递到函数中的任何参数名

作为解决此问题的一般方法,请查看堆栈跟踪或调用堆栈:

Test method GetApiModel threw exception: 
System.ArgumentNullException: Value cannot be null.
Parameter name: value
    at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
您可以看到,参数名称
反序列化对象
的第一个参数。这使我检查了反序列化JSON字符串的AutoMapper映射。该字符串在我的数据库中为空


您可以将代码更改为检查null。

检查
x
是否为
null
我很确定
ListMetadataKor
null
,因为错误说明
source
参数为
null
。source是您的可枚举项filtering@mrhands检查
ListMetadataKor
是否为空。@mrhand请突出显示代码的第430行。我只在安装在客户端的发布应用程序中遇到此错误。我在开发过程中从未遇到此错误。它第一次总是成功运行。但有时在其他计算机上它不起作用。我仍然感到困惑,因为我可以了解这里实际发生的情况。我不知道如何检查我的错误t、 因为它在发行版应用程序中运行。而不是在我的开发环境中。所以我认为我必须首先进行远程调试器,以了解真正的问题。但是您是否认为我的变量仍然以值
null
定位在内存中?@mrhands我100%确定
ListMetadataKor
null
。您应该从
Tools.ToolConvert.GetMetadata
方法,并检查它何时返回
null
。我相信如果table
dataMetaDataKOR
为空,它将返回null。在这种情况下,您应该返回空列表OK..您知道我的代码无法加载到backgroundworker中吗?我已经使用thread
BWHelper.Run
@mrha加载了它nds还有一个可能的问题是处理BWHelper.Run方法中的异常。如果
Console.WriteLine("Hello World");
IEnumerable<int> list = null;
list.Where(d => d ==4).FirstOrDefault();
Test method GetApiModel threw exception: 
System.ArgumentNullException: Value cannot be null.
Parameter name: value
    at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)