Yum*_*ume 4 c# nullreferenceexception invocationtargetexception windows-phone-8
我试图根据我从Node.js API获得的JSON响应,在命名的StackPanel中动态添加按钮.我将这个JSON数组解析为一个对象数组,该对象具有将所有属性放在适当位置的类.然后,在foreach中,我想使用我放在对象数组中的信息来构建按钮.请注意,对象数组包含良好的属性.如果我试着简单地MessageBox.Show(item.name)在我的foreach中做一个,它将显示我所有对象的所有名称.没有值为null,我检查了三次.
以下是我创建按钮的方法:
ItemList[] items = JsonConvert.DeserializeObject<ItemList[]>(jsonString);
Dispatcher.BeginInvoke(new Action(() =>
{
foreach (ItemList item in items)
{
Button ItemName = new Button();
//defining properties of the button, like width, etc..
ItemName.Content = item.title;
ItemName.Name = item.id;
//Will add Click event.
ItemPanel.Children.Add(ItemName);
}
}));
Run Code Online (Sandbox Code Playgroud)
在尝试时,我得到一个TargetInvocationException:
[System.Reflection.TargetInvocationException {System.Reflection.TargetInvocationException:调用目标抛出了异常.---> System.NullReferenceException:对象引用未设置为对象的实例.
这是为什么 ?神秘.我试着创建按钮对象并将其添加到我的StackPanel中而不做任何其他事情,并且错误是相同的.
我究竟做错了什么 ?如何摆脱这个?我已经在另一个应用程序中创建了这样的动态元素,这样做有效.
函数创建按钮在函数末尾调用,该函数使用HTTP请求从api获取JSON.在OnNavigatedTo方法中调用那个HTTP请求函数(是的,这很奇怪,但这是我发现在到达页面时自动调用函数的唯一方法,使用来自OnNavigatedTo方法的变量.我在其他页面上做过,而且它是工作(虽然它不是创建按钮,只是修改现有的文本块).
这是完整的错误:
System.Reflection.TargetInvocationException:
Exception has been thrown by the target of an invocation. --->
System.NullReferenceException: Object reference not set to an instance of an object.
at CubbyHole_Mobile.BrowseFiles.<>c__DisplayClass1.b__0()
--- End of inner exception stack trace ---
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
at System.Reflection.RuntimeMethodInfo.UnsafeInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Delegate.DynamicInvokeImpl(Object[] args)
at System.Windows.Threading.DispatcherOperation.Invoke()
Run Code Online (Sandbox Code Playgroud)
这是堆栈跟踪:
CubbyHole Mobile.DLL!CubbyHole_Mobile.App.Application_UnhandledException(object sender, System.Windows.ApplicationUnhandledExceptionEventArgs e) Ligne 100 C#
System.Windows.ni.dll!MS.Internal.Error.CallApplicationUEHandler(System.Exception e) Inconnu
System.Windows.ni.dll!MS.Internal.Error.IsNonRecoverableUserException(System.Exception ex, out uint xresultValue) Inconnu
System.Windows.ni.dll!System.Windows.Threading.DispatcherOperation.Invoke() Inconnu
System.Windows.ni.dll!System.Windows.Threading.Dispatcher.Dispatch(System.Windows.Threading.DispatcherPriority priority) Inconnu
System.Windows.ni.dll!System.Windows.Threading.Dispatcher.OnInvoke(object context) Inconnu
System.Windows.ni.dll!System.Windows.Hosting.CallbackCookie.Invoke(object[] args) Inconnu
System.Windows.RuntimeHost.ni.dll!System.Windows.RuntimeHost.ManagedHost.InvokeDelegate(System.IntPtr pHandle, int nParamCount, System.Windows.Hosting.NativeMethods.ScriptParam* pParams, System.Windows.Hosting.NativeMethods.ScriptParam* pResult) Inconnu
Run Code Online (Sandbox Code Playgroud)
(对不起,法国VS,但应该是可读的)
此外,尝试将新创建的项添加到现有StackPanel时会引发错误,但不会在该行上停止.它转到UnhandledException方法中的app.xaml.cs,然后使用Debugger.Break()停止; (就像我在处理这个问题时遇到的几乎所有错误一样).
Yum*_*ume 14
好的..我只是想通了.那不是代码错误.一点也不.好吧,实际上不是我的代码.
厌倦了徒劳的尝试,我保存并关闭了Visual Studio以休息一下.然后我回来了..它的确有效.没有任何接触.尝试修改我的代码十几次后,重新生成整个解决方案几次,最后通过重新启动VS解决了自己?上帝,我讨厌那种事情......因为有一天我会变得疯狂.
好吧,谢谢大家的回复!