I am trying to add buttons dynamically in a named StackPanel, according to a JSON response that I get from my Node.js API. I parse this JSON array into an object array, object having their class to put all the attributes in the good place. Then, in a foreach, I want to build buttons using the informations I put in my object array. Note that the object array contains the good attributes. If I try to simply do a MessageBox.Show(item.name)
in my foreach, it will display all the names of all my objects. No value is null, I checked three times.
Here is how I create the buttons :
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);
}
}));
And when trying, I get a TargetInvocationException :
[System.Reflection.TargetInvocationException {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.
Why is that ? Mystery. I tried just to create the button object and add it in my StackPanel without doing anything else, and the error is the same.
What am I doing wrong ? How to get rid of this ? I already made dynamic elements like that in another app, and that was working.
The function creating buttons is called at the end of the function that get the JSON from the api using an HTTP request. That HTTP request function is called in the OnNavigatedTo method (yeah, that's weird, but that's the only way I found to automatically call functions when the page is reached, using variables coming from the OnNavigatedTo method. I did it on other pages, and it's working (though it's not creating buttons, just modifying existing textblocks).
Here's the full error :
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()
And here is the stack trace :
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
(Sorry, french VS, but should be readable)
Also, the error is thrown when trying to add the newly created item to the existing StackPanel, but not stop on that line. It goes to app.xaml.cs in the UnhandledException method, and stop with the Debugger.Break(); (Like almost every error I had while working on this).
Okay.. I just figured it out. That wasn't a code error. Not at all. Well, not MY code actually.
Tired of trying in vain, I saved and closed Visual Studio to take a break. Then I came back and.. It works. Without touching anything. After trying to modify my code a dozen times, after regenerating the entire solution several times, and finally it solved itself by rebooting VS ? God I hate that kind of things.. I'll become crazy because of that one day.
Well, thanks to everyone replied !
Are you sure ItemPanel
isn't null? If everything crashes at the .Children.Add()
, that's the most likely scenario.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With