wpf 调用的目标已抛出 LINQ 和异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11809530/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 04:50:02  来源:igfitidea点击:

LINQ and Exception has been thrown by the target of an invocation

c#wpflinq

提问by Petree

I have WPF .NET FW4 application with linq and SQL CE 4.0. Randomly on customer's notebook I recieving this error:

我有带有 linq 和 SQL CE 4.0 的 WPF .NET FW4 应用程序。随机在客户的笔记本上我收到此错误:

Exception has been thrown by the target of an invocation.

调用的目标已抛出异常。

I already know, that important is Inner exception (with stack trace):

我已经知道,重要的是内部异常(带有堆栈跟踪):

Object reference not set to an instance of an object. at lambda_method(Closure )

你调用的对象是空的。在 lambda_method(Closure )

Code block:

代码块:

private void LoadCustomersDetail(CustomersView cw)
    {
        Persons customer = db.Persons.Where(c => c.Person_id == cw.Customer_id).FirstOrDefault();
        reg = new RegistrationWindow(db, customer, false);
        reg.ShowDialog();
        if (reg.DialogResult.Value)
        {
            if (!onlyDebtors)
                LoadCustomers();
            else
                LoadDebtors();
        }
    }

Stack trace for original exception:

原始异常的堆栈跟踪:

at System.RuntimeMethodHandle._InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeType typeOwner)
   at System.RuntimeMethodHandle.InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeType typeOwner)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
   at System.Delegate.DynamicInvokeImpl(Object[] args)
   at System.Data.Linq.CommonDataServices.GetKeyFromPredicate(MetaType type, Dictionary`2 keys, Expression mex, Expression vex)
   at System.Data.Linq.CommonDataServices.GetKeysFromPredicate(MetaType type, Dictionary`2 keys, Expression expr)
   at System.Data.Linq.CommonDataServices.GetKeyValues(MetaType type, LambdaExpression predicate)
   at System.Data.Linq.CommonDataServices.GetCachedObject(Expression query)
   at System.Data.Linq.CommonDataServices.GetCachedObject(Expression query)
   at System.Data.Linq.SqlClient.SqlProvider.GetCachedResult(Expression query)
   at System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query)
   at System.Data.Linq.DataQuery`1.System.Linq.IQueryProvider.Execute[S](Expression expression)
   at System.Linq.Queryable.FirstOrDefault[TSource](IQueryable`1 source)
   at ProjectName.Core.ViewWindow.LoadCustomersDetail(CustomersView cw)
   at ProjectName.Core.ViewWindow.dataGridCustomersList_MouseDoubleClick(Object sender, MouseButtonEventArgs e)
   at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
   at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
   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.Controls.Control.OnMouseDoubleClick(MouseButtonEventArgs e)
   at System.Windows.Controls.Control.HandleDoubleClick(Object sender, MouseButtonEventArgs e)
   at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
   at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
   at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
   at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
   at System.Windows.UIElement.ReRaiseEventAs(DependencyObject sender, RoutedEventArgs args, RoutedEvent newEvent)
   at System.Windows.UIElement.OnMouseDownThunk(Object sender, MouseButtonEventArgs e)
   at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
   at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
   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.RaiseTrustedEvent(RoutedEventArgs args)
   at System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted)
   at System.Windows.Input.InputManager.ProcessStagingArea()
   at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)
   at System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)
   at System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel)
   at System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, WindowMessage msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)

回答by H.B.

The exception seems to concern the Where-expression, so either cor cwis null. I would recommend an argument check at the beginning of the method (throw ArgumentNullExceptionif cw == null). Also make sure the Personscollection contains no null values (if that is even possible).

异常似乎与 -Where表达式有关,因此corcw为空。我建议在方法的开头进行参数检查(throw ArgumentNullExceptionif cw == null)。还要确保Persons集合不包含空值(如果可能的话)。

回答by John Woo

Your error is here cw.Customer_id. your cwis null.

你的错误在这里cw.Customer_id。你的cwnull