This is Xamarin's bug tracking system. For product support, please use the support links listed in your Xamarin Account.
Bug 53417 - Clearing Effects collection results in NullReferenceException
Summary: Clearing Effects collection results in NullReferenceException
Status: RESOLVED FIXED
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms (show other bugs)
Version: 2.3.3
Hardware: Macintosh Mac OS
: Normal major
Target Milestone: ---
Assignee: Chris King
URL:
Depends on:
Blocks:
 
Reported: 2017-03-16 21:15 UTC by Tim Klingeleers
Modified: 2017-03-17 23:05 UTC (History)
4 users (show)

See Also:
Tags: effect clear crash ac
Is this bug a regression?: ---
Last known good build:


Attachments
Solution that shows the issue, originally from Lesson 2 of Xamarin University XAM330 (449.19 KB, application/zip)
2017-03-16 21:24 UTC, Tim Klingeleers
Details

Description Tim Klingeleers 2017-03-16 21:15:01 UTC
# Steps to reproduce
- Start with the solution from XAM330 of Xamarin University (https://university.xamarin.com/materials/using-effects-in-xamarinforms-xam330) under: Lab Materials/Part 02 Resources/Completed/ControlExplorer.sln
- Change line 28 in MainPage.xaml.cs of the PCL project (ControlExplorer) from 
labelWelcome.Effects.Remove(fontEffect);
to
labelWelcome.Effects.Clear();
- Run the application and turn the switch to the off-position

# Expected behavior
- Clear all effects on a control without NullReferenceExceptions

# Actual behavior
- A NullReferenceException is thrown because the EffectsOnClearing method of the Element class loops over all effects and calls the ClearEffect on it. But the Effects collection also contains a null Effect initially.

# Supplemental info (logs, images, videos)
Stacktrace of NullReferenceException:
  at Xamarin.Forms.Element.EffectsOnClearing (System.Object sender, System.EventArgs eventArgs) [0x00014] in C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Core\Element.cs:523 
  at Xamarin.Forms.TrackableCollection`1[T].ClearItems () [0x00000] in C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Core\TrackableCollection.cs:12 
  at System.Collections.ObjectModel.Collection`1[T].Clear () [0x00017] in /Users/builder/data/lanes/4009/3a62f1ea/source/mono/mcs/class/referencesource/mscorlib/system/collections/objectmodel/collection.cs:75 
  at ControlExplorer.MainPage.OnSwitchToggled (System.Object sender, Xamarin.Forms.ToggledEventArgs e) [0x0000c] in /Users/tim/Downloads/using-effects-in-xamarinforms-xam330/Lab Materials/Part 02 Resources/Completed/ControlExplorer/ControlExplorer/MainPage.xaml.cs:28 
  at (wrapper delegate-invoke) System.EventHandler`1[Xamarin.Forms.ToggledEventArgs]:invoke_void_object_TEventArgs (object,Xamarin.Forms.ToggledEventArgs)
  at Xamarin.Forms.Switch+<>c.<.cctor>b__10_0 (Xamarin.Forms.BindableObject bindable, System.Object oldValue, System.Object newValue) [0x0000f] in C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Core\Switch.cs:13 
  at Xamarin.Forms.BindableObject.SetValueActual (Xamarin.Forms.BindableProperty property, Xamarin.Forms.BindableObject+BindablePropertyContext context, System.Object value, System.Boolean currentlyApplying, Xamarin.Forms.BindableObject+SetValueFlags attributes, System.Boolean silent) [0x00108] in C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Core\BindableObject.cs:584 
  at Xamarin.Forms.BindableObject.SetValueCore (Xamarin.Forms.BindableProperty property, System.Object value, Xamarin.Forms.BindableObject+SetValueFlags attributes, Xamarin.Forms.BindableObject+SetValuePrivateFlags privateAttributes) [0x0014b] in C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Core\BindableObject.cs:378 
  at Xamarin.Forms.BindableObject.SetValueCore (Xamarin.Forms.BindableProperty property, System.Object value, Xamarin.Forms.BindableObject+SetValueFlags attributes) [0x00000] in C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Core\BindableObject.cs:324 
  at Xamarin.Forms.Element.Xamarin.Forms.IElementController.SetValueFromRenderer (Xamarin.Forms.BindableProperty property, System.Object value) [0x00000] in C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Core\Element.cs:249 
  at Xamarin.Forms.Platform.Android.SwitchRenderer.Android.Widget.CompoundButton.IOnCheckedChangeListener.OnCheckedChanged (Android.Widget.CompoundButton buttonView, System.Boolean isChecked) [0x00000] in C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\Renderers\SwitchRenderer.cs:16 
  at Android.Widget.CompoundButton+IOnCheckedChangeListenerInvoker.n_OnCheckedChanged_Landroid_widget_CompoundButton_Z (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_buttonView, System.Boolean isChecked) [0x00011] in /Users/builder/data/lanes/4009/3a62f1ea/source/monodroid/src/Mono.Android/platforms/android-23/src/generated/Android.Widget.CompoundButton.cs:86 
  at (wrapper dynamic-method) System.Object:2d66780f-670c-4e89-acdb-9b74746becb1 (intptr,intptr,intptr,bool)

# Test environment (full version information)
Xamarin Forms: 2.3.3.180
Xamarin Studio: 6.2
Android: any version
iOS: any version
OS: macOS Sierra
Comment 1 Tim Klingeleers 2017-03-16 21:24:29 UTC
Created attachment 20393 [details]
Solution that shows the issue, originally from Lesson 2 of Xamarin University XAM330
Comment 2 Paul DiPietro 2017-03-17 02:38:08 UTC
Checked the project against 2.3.4-pre5 for good measure and the crash still appears to be occurring.
Comment 4 Rui Marinho 2017-03-17 23:05:38 UTC
Should be fixed on 2.3.5-pre1

Note You need to log in before you can comment on or make changes to this bug.