Node.js API에서 가져온 JSON 응답에 따라 이름이 지정된 StackPanel에 버튼을 동적으로 추가하려고합니다. 이 JSON 배열을 객체 배열로 구문 분석합니다. 객체는 해당 클래스를 사용하여 모든 속성을 좋은 위치에 배치합니다. 그런 다음 foreach에서는 객체 배열에 넣은 정보를 사용하여 버튼을 만들고 싶습니다. 객체 배열에는 좋은 속성이 들어 있습니다. foreach에서 MessageBox.Show(item.name)
을 단순히 수행하려고하면 내 모든 객체의 이름이 모두 표시됩니다. 값이 없으면 세 번 확인합니다.호출 대상에 의해 예외가 발생합니다. 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);
}
}));
그리고하려고 할 때, 나는 TargetInvocationException 얻을 :
[System.Reflection.TargetInvocationException {System.Reflection.TargetInvocationException : 예외가있다 여기
내가 버튼을 만드는 방법이다 호출의 타겟에 의해 슬로우됩니다. ---> System.NullReferenceException : 개체 참조가 개체의 인스턴스로 설정되지 않았습니다.
왜 그렇습니까? 신비. 버튼 객체를 만들고 다른 것을하지 않고 StackPanel에 추가하려고했는데 오류가 동일합니다.
내가 뭘 잘못하고 있니? 이걸 없애는 방법? 이미 다른 앱에서 동적 요소를 만들었고 작동 중이었습니다.
함수를 만드는 함수는 HTTP 요청을 사용하여 API에서 JSON을 가져 오는 함수의 끝에서 호출됩니다. 그 HTTP 요청 함수는 OnNavigatedTo 메서드에서 호출됩니다. (이상하게 들리지만 OnNavigatedTo 메서드에서 온 변수를 사용하여 페이지에 도달하면 자동으로 함수를 호출하는 방법입니다. 다른 페이지에서 그 함수를 호출했습니다. . (을 그냥 기존 textblocks을 수정 버튼을 생성하지 비록) 작업
것은 여기에 전체 오류입니다 :
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()
그리고 여기에 스택 추적입니다 :
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
(죄송합니다, 프랑스어 VS, 그러나 읽을 수 있어야한다)
또한 새로 작성한 항목을 기존 StackPanel에 추가하려고 시도하지만 해당 줄에서 중지하지 않으면 오류가 발생합니다. UnhandledException 메서드의 app.xaml.cs로 이동하고 Debugger.Break(); (내가이 작업을하는 동안 가지고있는 거의 모든 오류처럼).
'NullReferenceException'의 거의 모든 사례가 동일합니다. 일부 힌트는 "[.NET의 NullReferenceException은 무엇입니까?] (http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)"를 참조하십시오. –
스택 추적은 어디에 있습니까? 전체 예외 출력은 어디에 있습니까? –
어떤 라인이 예외를 던지고 있습니까? 더 많은 정보가 필요합니다. –