Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
234 views
in Technique[技术] by (71.8m points)

c# - How to resolve Value cannot be null. Parameter name: source in linq?

I don't know why I get this kind of error. It happens sometimes, and I suspicious of my code that still have thread running while I close my Application. So when I open again it happens.

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:handitaOfficeProjectSusenas 2015Aplikasi Template SurveiSusenas2015ViewModelsKuesionerVMVsen15_KVal.cs:line 430
???at Susenas2015.ViewModels.Kuesioner.VMVsen15_KVal.vSen15_K_Loaded(Object sender, RoutedEventArgs e) in d:handitaOfficeProjectSusenas 2015Aplikasi Template SurveiSusenas2015ViewModelsKuesionerVMVsen15_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`

My code here it is

    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");
            }

        }

When I start my application for my first time, it doesn't get any error. It happens when I open in 2nd or more. And if I open my application it would stuck in that code.

How I can solve this? I'm pretty sure that my Property ListMetadataKor is not null

And ListMetadataKor is instance of List<Metadata> object that I have created. It happens only in rare cases. And I don't know to solve it

UPDATE

This is my code in imageenter image description here

I fill ListMetadataKor with this code

BWHelper.Run((s, e) =>
{
    DataTable dataMetaDataKOR = ExcelHelper.GetDataTableFromExcel(
        AppConstants.FILE_METADATA, AppConstants.SHEET_METADATA_KOR
    );

    DataTable dataKonsistensiKOR = ExcelHelper.GetDataTableFromExcel(
         AppConstants.FILE_METADATA, AppConstants.SHEET_KONSISTENSI_KOR
     );

    listKonsistensiKor = Tools.ToolConvert.GetKonsistensi(dataKonsistensiKOR);
    listMetadataKor = Tools.ToolConvert.GetMetadata(dataMetaDataKOR);

    foreach (Metadata metadata in listMetadataKor)
    {
        metadata.Range.ProsesRange();
    }

}, (s, e) =>
{
    try
    {
        kor = new VSEN15_K() { Title = "Validasi Susenas - KOR" };
        kor.DataContext = new VMVsen15_KVal(rtSusenas.MasterRT, kor, this, listKonsistensiKor, listMetadataKor);
        kor.PreviewKeyDown += EventsCollection.EnterAsTabPreviewKeyDown;
        vmHome.HideLoading();
        UpdateMetaDataEntriKOR(RTSusenas.MasterRT);
        kor.ShowDialog();
    }
    catch (Exception Ex)
    {
        vmHome.HideLoading();
        Dialog.Error(Ex);
    }
});

And then I throw the variable through constructor of my class

 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;

My tools konsistensi like this

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;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Error message clearly says that source parameter is null. Source is the enumerable you are enumerating. In your case it is ListMetadataKor object. And its definitely null at the time you are filtering it second time. Make sure you never assign null to this list. Just check all references to this list in your code and look for assignments.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...