Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throws NullReferenceException caused by ImageService is null #3

Open
NishiokaTakeo opened this issue Nov 16, 2023 · 11 comments
Open

Throws NullReferenceException caused by ImageService is null #3

NishiokaTakeo opened this issue Nov 16, 2023 · 11 comments

Comments

@NishiokaTakeo
Copy link
Contributor

Description

ImageService is null as a result, it causes NullReferenceException.

Steps to Reproduce

Add a file contains below.

<?xml version="1.0" encoding="utf-8" ?>
<ContentView
    x:Class="App.Infrastructure.Components.RoundImage"
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:ffsvg="clr-namespace:FFImageLoading.Maui;assembly=FFImageLoading.Maui"
    xmlns:fftransformations="clr-namespace:FFImageLoading.Transformations;assembly=FFImageLoading.Maui"
    x:Name="This">
    <ffsvg:CachedImage
        Aspect="AspectFit"
        DownsampleToViewSize="true"
        ErrorPlaceholder="{Binding FallbackSource, Source={x:Reference This}}"
        HorizontalOptions="FillAndExpand"
        LoadingPlaceholder="{Binding FallbackSource, Source={x:Reference This}}"
        Source="{Binding EffectiveSource, Source={x:Reference This}}"
        VerticalOptions="Fill">
        <ffsvg:CachedImage.Transformations>
            <fftransformations:CircleTransformation />
        </ffsvg:CachedImage.Transformations>
    </ffsvg:CachedImage>
</ContentView>

Set Source binding property from behind the code. At this time, It was Stream. but it does not matter as ImageService was null.

Expected Behavior

Should not throws NullReferenceException.

Actual Behavior

throws NullReferenceException.

Basic Information

  • Version with issue: 1.0.3
  • Last known good version:
  • Platform: Maui with .net 8

Screenshots

image

@NishiokaTakeo NishiokaTakeo changed the title ImageService is null Throws NullReferenceException caused by ImageService is null Nov 16, 2023
@NishiokaTakeo
Copy link
Contributor Author

It could FindMauiContext() returns null.

@ziomek64
Copy link

Is it supported already in .net 8 maui? I raised an issue about it and it's coming

@microspaze
Copy link
Owner

microspaze commented Nov 19, 2023

@NishiokaTakeo I have reviewed your merge request, your code can prevent NRE but still not fix the cause of NRE. The better fix is to find out why FindMauiContext() returns null. Can you upload the full demo project then I can take a deeper look for this issue.

@brunck
Copy link

brunck commented Dec 15, 2023

I found this to be the case in my app because I was setting MainPage for my App class too early. Application.Current.Handler wasn't set yet when I was setting MainPage and that caused FindMauiContext() to return null.
I worked around this by setting MainPage in an overload of CreateWindow in my application class. By that time, Application.Current.Handler was properly set. The takeaway is that you have to be conscious of when your pages that have CachedImages on them are being created. It's at that time that FindMauiContext() is being called.

This probably is not the solution but perhaps it gives some insight as to why FindMauiContext() might be returning null. It might not be looking in the right places, or in the right places at quite the right time.

@NishiokaTakeo
Copy link
Contributor Author

Hi @brunck , Thanks for reply and the workaround works for my case as well.

I was setting MainPage for my App class too early. Application.Current.Handler wasn't set yet when I was setting MainPage and that caused FindMauiContext() to return null.

@microspaze,

As @brunck pointed out, I think FFImageLoading.Maui library should take care into above case and the library should not throw exception.

MS document supposes to set MainPage in App constructor. But not strongly mention as xamarin document does.

For Xamarin, document mentioned,

The MainPage property should be set in the App constructor,

It would be great if you revaluate PR goes in.

@microspaze
Copy link
Owner

I have merged the push request. Thanks very much!

@microspaze
Copy link
Owner

The latest version 1.0.7 has been uploaded to NUGET. You can try it to verify this issue.

@petroskin
Copy link

Had this problem in an application, and the workaround posted from @brunck helped out.

After updating FFImageLoading.Maui to 1.0.7 and reverting the workaround changes, the app is working as expected and images are showing up normally.

@NishiokaTakeo
Copy link
Contributor Author

I confirmed the issue does not happen. Thank you for fixing!

@Jasper7
Copy link

Jasper7 commented Aug 30, 2024

@NishiokaTakeo

Since version 1.2.6 the problem has returned
ImageService.Instance is null

In version 1.2.5 is good

Please check this

@microspaze
Copy link
Owner

microspaze commented Aug 30, 2024

@Jasper7 I really don't know what had been changed in Maui. But if you upgrade Maui to the latest version, then those weird
problems will all disappear. JUST TRY: sudo dotnet workload update

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants