Xfx.Controls icon indicating copy to clipboard operation
Xfx.Controls copied to clipboard

On mainpage change I got error Object reference not set to an instance of an object

Open JFDionne opened this issue 5 years ago • 11 comments

Hi, thank for this controls. When I change my App.mainpage to something else, the app crash and I got this error:

Object reference not set to an instance of an object

at Xfx.Controls.iOS.Renderers.XfxEntryRendererTouch.OnElementChanged (Xamarin.Forms.Platform.iOS.ElementChangedEventArgs1[TElement] e) [0x00015] in <f2149ff8a79944c98fa707a3abffd0b5>:0 at Xamarin.Forms.Platform.iOS.VisualElementRenderer1[TElement].SetElement (TElement element) [0x0012a] in <db147fad2aa9412c845ff0ead92dfe20>:0 at Xamarin.Forms.Platform.iOS.VisualElementRenderer1[TElement].Dispose (System.Boolean disposing) [0x0008c] in <db147fad2aa9412c845ff0ead92dfe20>:0 at Xamarin.Forms.Platform.iOS.ViewRenderer2[TView,TNativeView].Dispose (System.Boolean disposing) [0x0005c] in <db147fad2aa9412c845ff0ead92dfe20>:0 at Foundation.NSObject.Dispose () [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.8.0.2/src/Xamarin.iOS/Foundation/NSObject2.cs:147 at Xamarin.Forms.Platform.iOS.VisualElementPackager.Dispose (System.Boolean disposing) [0x0003f] in D:\a\1\s\Xamarin.Forms.Platform.iOS\VisualElementPackager.cs:69 at Xamarin.Forms.Platform.iOS.VisualElementPackager.Dispose () [0x00000] in D:\a\1\s\Xamarin.Forms.Platform.iOS\VisualElementPackager.cs:37 at Xamarin.Forms.Platform.iOS.VisualElementRenderer1[TElement].Dispose (System.Boolean disposing) [0x00058] in <db147fad2aa9412c845ff0ead92dfe20>:0 at Foundation.NSObject.Dispose () [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.8.0.2/src/Xamarin.iOS/Foundation/NSObject2.cs:147 at Xamarin.Forms.Platform.iOS.VisualElementPackager.Dispose (System.Boolean disposing) [0x0003f] in D:\a\1\s\Xamarin.Forms.Platform.iOS\VisualElementPackager.cs:69 at Xamarin.Forms.Platform.iOS.VisualElementPackager.Dispose () [0x00000] in D:\a\1\s\Xamarin.Forms.Platform.iOS\VisualElementPackager.cs:37 at Xamarin.Forms.Platform.iOS.VisualElementRenderer1[TElement].Dispose (System.Boolean disposing) [0x00058] in <db147fad2aa9412c845ff0ead92dfe20>:0 at Foundation.NSObject.Dispose () [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.8.0.2/src/Xamarin.iOS/Foundation/NSObject2.cs:147 at Xamarin.Forms.Platform.iOS.VisualElementPackager.Dispose (System.Boolean disposing) [0x0003f] in D:\a\1\s\Xamarin.Forms.Platform.iOS\VisualElementPackager.cs:69 at Xamarin.Forms.Platform.iOS.VisualElementPackager.Dispose () [0x00000] in D:\a\1\s\Xamarin.Forms.Platform.iOS\VisualElementPackager.cs:37 at Xamarin.Forms.Platform.iOS.VisualElementRenderer1[TElement].Dispose (System.Boolean disposing) [0x00058] in <db147fad2aa9412c845ff0ead92dfe20>:0 at Foundation.NSObject.Dispose () [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.8.0.2/src/Xamarin.iOS/Foundation/NSObject2.cs:147 at Xamarin.Forms.Platform.iOS.VisualElementPackager.Dispose (System.Boolean disposing) [0x0003f] in D:\a\1\s\Xamarin.Forms.Platform.iOS\VisualElementPackager.cs:69 at Xamarin.Forms.Platform.iOS.VisualElementPackager.Dispose () [0x00000] in D:\a\1\s\Xamarin.Forms.Platform.iOS\VisualElementPackager.cs:37 at Xamarin.Forms.Platform.iOS.VisualElementRenderer1[TElement].Dispose (System.Boolean disposing) [0x00058] in <db147fad2aa9412c845ff0ead92dfe20>:0 at Foundation.NSObject.Dispose () [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.8.0.2/src/Xamarin.iOS/Foundation/NSObject2.cs:147 at Xamarin.Forms.Platform.iOS.VisualElementPackager.Dispose (System.Boolean disposing) [0x0003f] in D:\a\1\s\Xamarin.Forms.Platform.iOS\VisualElementPackager.cs:69 at Xamarin.Forms.Platform.iOS.VisualElementPackager.Dispose () [0x00000] in D:\a\1\s\Xamarin.Forms.Platform.iOS\VisualElementPackager.cs:37 at Xamarin.Forms.Platform.iOS.VisualElementRenderer1[TElement].Dispose (System.Boolean disposing) [0x00058] in <db147fad2aa9412c845ff0ead92dfe20>:0 at Foundation.NSObject.Dispose () [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.8.0.2/src/Xamarin.iOS/Foundation/NSObject2.cs:147 at Xamarin.Forms.Platform.iOS.DisposeHelpers.DisposeModalAndChildRenderers (Xamarin.Forms.Element view) [0x00041] in D:\a\1\s\Xamarin.Forms.Platform.iOS\DisposeHelpers.cs:23 at Xamarin.Forms.Platform.iOS.Platform.System.IDisposable.Dispose () [0x00048] in D:\a\1\s\Xamarin.Forms.Platform.iOS\Platform.cs:83 at Xamarin.Forms.Platform.iOS.FormsApplicationDelegate.UpdateMainPage () [0x00022] in D:\a\1\s\Xamarin.Forms.Platform.iOS\FormsApplicationDelegate.cs:181 at Xamarin.Forms.Platform.iOS.FormsApplicationDelegate.ApplicationOnPropertyChanged (System.Object sender, System.ComponentModel.PropertyChangedEventArgs args) [0x00012] in D:\a\1\s\Xamarin.Forms.Platform.iOS\FormsApplicationDelegate.cs:142 at Xamarin.Forms.BindableObject.OnPropertyChanged (System.String propertyName) [0x00000] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:211 at Xamarin.Forms.Element.OnPropertyChanged (System.String propertyName) [0x00000] in D:\a\1\s\Xamarin.Forms.Core\Element.cs:353 at Xamarin.Forms.Application.set_MainPage (Xamarin.Forms.Page value) [0x00092] in D:\a\1\s\Xamarin.Forms.Core\Application.cs:105 at FTAC.CS.clsWebservice+<>c.<Verification_Login>b__1_2 () [0x00017] in C:\Dev\5\FTAC\FTAC\FTAC\CS\clsWebservice.cs:37 at Foundation.NSAsyncActionDispatcher.Apply () [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.8.0.2/src/Xamarin.iOS/Foundation/NSAction.cs:152 at (wrapper managed-to-native) UIKit.UIApplication.UIApplicationMain(int,string[],intptr,intptr) at UIKit.UIApplication.Main (System.String[] args, System.IntPtr principal, System.IntPtr delegate) [0x00005] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.8.0.2/src/Xamarin.iOS/UIKit/UIApplication.cs:79 at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0002c] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.8.0.2/src/Xamarin.iOS/UIKit/UIApplication.cs:63 at FTAC.iOS.Application.Main (System.String[] args) [0x00002] in C:\Dev\5\FTAC\FTAC\FTAC.iOS\Main.cs:19

JFDionne avatar May 22 '19 04:05 JFDionne

can you please give me a working repro of this issue?

ChaseFlorell avatar May 22 '19 12:05 ChaseFlorell

Yes, there we go: https://github.com/JFDionne/Xfx.Controls.Test

But it's pretty simple, just create a new project, add your controls, put a newpage and run :)

*Update You just need to take your project, update from Form 3.5 to 4.0 and with your example it will do the same error.

JFDionne avatar May 24 '19 04:05 JFDionne

@ChaseFlorell this is because in xamarin 4 Dispose is called correctly on the renderer (wonder why it didn't in previous versions though)... Control is null when getting in OnElementChanged, so the unsubscribes fail. So the onelementchanged should check if Control != null where applicable

I use this customrenderer as temporary workaround (reset element before control is reset):

public class CustomXfxEntryRendererTouch : XfxEntryRendererTouch
    {
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                SetElement(null); //trigger elementchange before base dispose happens https://github.com/XamFormsExtended/Xfx.Controls/issues/97
            }

            base.Dispose(disposing);
        }
    }

csteeg avatar Jun 04 '19 12:06 csteeg

Late comment... but I ran into the same error using Xamarin.Forms 4.X on iOS.

My solution was to downgrade Xamarin.Forms to version 3.5.0.129452 (which is the version this package is built on).

j-h-m avatar Sep 11 '19 02:09 j-h-m

Make sure to add the ExportRenderer. I'm brand spanking new to xamarin and C#, so I figured this would help those still learning. Feed control being used in page (XfxEntry) and your custom renderer (CustomXfxEntryRendererTouch).

[assembly: ExportRenderer(typeof(XfxEntry), typeof(CustomXfxEntryRendererTouch))]
namespace Application.iOS.CustomRenderer
{
    public class CustomXfxEntryRendererTouch : XfxEntryRendererTouch
    {
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                SetElement(null); //trigger elementchange before base dispose happens https://github.com/XamFormsExtended/Xfx.Controls/issues/97
            }

            base.Dispose(disposing);
        }
    }
}

JimmyBarrientos avatar Oct 10 '19 17:10 JimmyBarrientos

I did implement the CustomXfxEntryRendererTouch but i'm still getting the same error any ideas?

JackMakhoul avatar Nov 19 '19 15:11 JackMakhoul

Make sure to add the ExportRenderer. I'm brand spanking new to xamarin and C#, so I figured this would help those still learning. Feed control being used in page (XfxEntry) and your custom renderer (CustomXfxEntryRendererTouch).

[assembly: ExportRenderer(typeof(XfxEntry), typeof(CustomXfxEntryRendererTouch))]
namespace Application.iOS.CustomRenderer
{
    public class CustomXfxEntryRendererTouch : XfxEntryRendererTouch
    {
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                SetElement(null); //trigger elementchange before base dispose happens https://github.com/XamFormsExtended/Xfx.Controls/issues/97
            }

            base.Dispose(disposing);
        }
    }
}

This solved my problem, thanks @JimmyBarrientos

rubberydev avatar Dec 05 '19 19:12 rubberydev

Using Xamarin Forms 4.3, I've tried the above but the CustomXfxEntryRendererTouch doesn't get instantiated. I've added the assembly too. Still getting the same error. Does anybody have any more ideas please?

butter5 avatar Dec 10 '19 15:12 butter5

Using Xamarin Forms 4.3, I've tried the above but the CustomXfxEntryRendererTouch doesn't get instantiated. I've added the assembly too. Still getting the same error. Does anybody have any more ideas please?

Someone needs to update this library to run on a newer version of XF. I wound up using an entry and listview on a separate page activated by a button to get the autocomplete functionality.

j-h-m avatar Dec 10 '19 16:12 j-h-m

I'm the owner of the code, unfortunately I haven't had the need to use this for quite some time, so I've let it fall into disrepair. I'd like to continue maintaining it, it's just a pile of time that I don't have. I do take PR's, however I'll still need to put some effort into deploying nuget updates.

ChaseFlorell avatar Dec 10 '19 18:12 ChaseFlorell

@JackMakhoul, @j-h-m, I had to create a new class inheriting from XfxEntry, and asociate the renderer with it:

namespace Application.Controls
{
    public class CustomXfxEntry : XfxEntry
    {

    }
}
[assembly: ExportRenderer(typeof(CustomXfxEntry), typeof(CustomXfxEntryRendererTouch))]
namespace Application.iOS.Renderers
{
    public class CustomXfxEntryRendererTouch : XfxEntryRendererTouch
    {
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                SetElement(null); //trigger elementchange before base dispose happens https://github.com/XamFormsExtended/Xfx.Controls/issues/97
            }

            base.Dispose(disposing);
        }
    }
}

Obviously in your page you need to use this control instead of the original one, and you have to go through the same process if you're using XfxComboBox too.

tiberiup avatar Feb 13 '20 15:02 tiberiup