I am getting below exception when loading my MAUI application.
System.NullReferenceException: 'Object reference not set to an instance of an object.'
[mono-rt] [ERROR] FATAL UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object.
[mono-rt] at FFImageLoading.Maui.CachedImage.SetupOnBeforeImageLoading(TaskParameter& imageLoader, IImageSourceBinding source, IImageSourceBinding loadingPlaceholderSource, IImageSourceBinding errorPlaceholderSource)
[mono-rt] at FFImageLoading.Maui.Platform.CachedImageHandler.UpdateBitmap(CachedImageView imageView, CachedImage image, CachedImage previousImage)
[mono-rt] at FFImageLoading.Maui.Platform.CachedImageHandler.ConnectHandler(CachedImageView platformView)
[mono-rt] at Microsoft.Maui.Handlers.ViewHandler`2[[FFImageLoading.Maui.CachedImage, FFImageLoading.Compat.Maui, Version=3.0.0.0, Culture=neutral, PublicKeyToken=null],[FFImageLoading.Maui.Platform.CachedImageView, FFImageLoading.Compat.Maui, Version=3.0.0.0, Culture=neutral, PublicKeyToken=null]].OnConnectHandler(View platformView) in D:\a\_work\1\s\src\Core\src\Handlers\View\ViewHandlerOfT.cs:line 76
[mono-rt] at Microsoft.Maui.Handlers.ViewHandler.OnConnectHandler(Object platformView) in D:\a\_work\1\s\src\Core\src\Handlers\View\ViewHandler.cs:line 204
[mono-rt] at Microsoft.Maui.Handlers.ElementHandler.ConnectHandler(Object platformView) in D:\a\_work\1\s\src\Core\src\Handlers\Element\ElementHandler.cs:line 106
[mono-rt] at Microsoft.Maui.Handlers.ElementHandler.SetVirtualView(IElement view) in D:\a\_work\1\s\src\Core\src\Handlers\Element\ElementHandler.cs:line 64
[mono-rt] at Microsoft.Maui.Handlers.ViewHandler`2[[FFImageLoading.Maui.CachedImage, FFImageLoading.Compat.Maui, Version=3.0.0.0, Culture=neutral, PublicKeyToken=null],[FFImageLoading.Maui.Platform.CachedImageView, FFImageLoading.Compat.Maui, Version=3.0.0.0, Culture=neutral, PublicKeyToken=null]].SetVirtualView(IView view) in D:\a\_work\1\s\src\Core\src\Handlers\View\ViewHandlerOfT.cs:line 53
[mono-rt] at Microsoft.Maui.Handlers.ViewHandler`2[[FFImageLoading.Maui.CachedImage, FFImageLoading.Compat.Maui, Version=3.0.0.0, Culture=neutral, PublicKeyToken=null],[FFImageLoading.Maui.Platform.CachedImageView, FFImageLoading.Compat.Maui, Version=3.0.0.0, Culture=neutral, PublicKeyToken=null]].SetVirtualView(IElement view) in D:\a\_work\1\s\src\Core\src\Handlers\View\ViewHandlerOfT.cs:line 56
[mono-rt] at Microsoft.Maui.Controls.Element.SetHandler(IElementHandler newHandler) in D:\a\_work\1\s\src\Controls\src\Core\Element\Element.cs:line 921
[mono-rt] at Microsoft.Maui.Controls.Element.set_Handler(IElementHandler value) in D:\a\_work\1\s\src\Controls\src\Core\Element\Element.cs:line 863
[mono-rt] at Microsoft.Maui.Controls.VisualElement.Microsoft.Maui.IElement.set_Handler(IElementHandler value) in D:\a\_work\1\s\src\Controls\src\Core\VisualElement\VisualElement.cs:line 2031
[mono-rt] at Microsoft.Maui.Platform.ElementExtensions.ToHandler(IElement view, IMauiContext context) in D:\a\_work\1\s\src\Core\src\Platform\ElementExtensions.cs:line 96
[mono-rt] at Microsoft.Maui.Platform.ElementExtensions.ToPlatform(IElement view, IMauiContext context) in D:\a\_work\1\s\src\Core\src\Platform\ElementExtensions.cs:line 127
[mono-rt] at Microsoft.Maui.Handlers.LayoutHandler.SetVirtualView(IView view) in D:\a\_work\1\s\src\Core\src\Handlers\Layout\LayoutHandler.Android.cs:line 41
[mono-rt] at Microsoft.Maui.Handlers.ViewHandler`2[[Microsoft.Maui.ILayout, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Platform.LayoutViewGroup, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].SetVirtualView(IElement view) in D:\a\_work\1\s\src\Core\src\Handlers\View\ViewHandlerOfT.cs:line 56
This exception is happening only on a particular scenario.
Scenario 1 (No Issue): Initially I will show login page on the app and after login go to the home page.
Scenario 2 (Has above exception): If the user is already logged in I will redirect the user to home page directly. I have implemented this by using a preferences key value. When the user logs in, I set a preference value to "true," and based on this preference value, I show the home page or the login page.
I initially placed the logic to check the preference value on the login page. However, this caused the login page to be displayed briefly before navigating to the home page, which is not a good user experience. To address this, I moved the logic to check the preference value on the App.xaml.cs
file. However, this change led to a "System.NullReferenceException".
The logic added on the App.xaml.cs:
try
{
string login = Preferences.Default.Get("islogin", "false").ToString();
if (login == "true")
{
MainPage = new HomePage(true);
}
else if (login == "false")
{
MainPage = new LoginPage();
}
}
catch (Exception e)
{
Debug.WriteLine("IsLogin::" + e);
}
I have added UseFFImageLoading
on the MauiProgram.cs class.
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
})
.UseFFImageLoading()
.UseMauiCompatibility()
How to resolve this issue?
UPDATE
I have issue with redirecting the pages to MainPage
or HomePage
. If I load the MainPage
from App.xaml.cs
like below no issue.
public partial class App : Application
{
public App()
{
InitializeComponent();
MainPage = new MainPage();
}
}
But if I load the HomePage
from App.xaml.cs
like below I am getting the above exception related to FFImageLoading
.
public partial class App : Application
{
public App()
{
InitializeComponent();
MainPage = new HomePage();
}
}
HomePage has one ffimageloading
widget and I have added .UseFFImageLoading()
property on the MauiProgram.cs
.
From the error log, I found some keyword about FFImageLoading.Compat.Maui
. But they had already changed all namespace about FFImageLoading.Compat.Maui
to FFImageLoading.Maui
. Are you using the right lib from https://www.nuget.org/packages/FFImageLoading.Maui.
Maybe you can try some actions like below: