Null Reference Exception when autosizing ListView

I have Syncfusion version 16.3.0.29 and Xamarin Forms 3.3.0.967583.  My code has worked with older versions.

The ListView is defined in XAML:

    <ContentPage.BindingContext>
        <local:DocketsInfoRepository />
    </ContentPage.BindingContext>

    <syncfusion:SfListView x:Name="listView" 
                               BackgroundColor="#F0F0F0"
                               ItemSpacing="10,10,10,10"
                               ItemsSource="{Binding DocketsInfo}" 
                               AutoFitMode="Height"
                               ItemSize="200">
        <syncfusion:SfListView.LayoutManager>
            <syncfusion:LinearLayout />
        </syncfusion:SfListView.LayoutManager>
        <syncfusion:SfListView.ItemTemplate>
            <DataTemplate>
                <Grid Padding="10" BackgroundColor="White">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>
                    <Label Grid.Row="0" Text="{Binding DocketNumber}" />
                    <Label Grid.Row="1" Text="{Binding DocketType}" />
                    <Label Grid.Row="2" Text="{Binding SentencingDate}" />
                    <Label Grid.Row="3" Text="{Binding SentencingDescription}" />
                    <Label Grid.Row="4" Text="{Binding Disposition}" />
                    <Label Grid.Row="5" Text="{Binding DispositionDate}" />
                    <Label Grid.Row="6" Text="{Binding ChargeDescription}" />
                </Grid>
            </DataTemplate>
        </syncfusion:SfListView.ItemTemplate>
    </syncfusion:SfListView>


The DocketsInfo object is being correctly instantiated with data.  Whenever the page loads, the application crashes with the following stack trace:

System.NullReferenceException: Object reference not set to an instance of an object

  at Syncfusion.ListView.XForms.LayoutBase.GetAutoFitItemSize (Syncfusion.ListView.XForms.ListViewItemInfoBase itemInfo, System.Double& itemSize, System.Double itemSpacing, System.Int32 spanCount) [0x000cb] in <9dbe1f5143c64addbe242929584a687b>:0

  at Syncfusion.ListView.XForms.LinearLayout.GetAutoFitItemSize (Syncfusion.ListView.XForms.ListViewItemInfoBase itemInfo, System.Double& itemSize, System.Double itemSpacing) [0x00000] in <9dbe1f5143c64addbe242929584a687b>:0

  at Syncfusion.ListView.XForms.LinearLayout.SetRowHeight (Syncfusion.ListView.XForms.ListViewItemInfo itemInfo, System.Int32 index, System.Double& size, System.Double itemSpacing) [0x00011] in <9dbe1f5143c64addbe242929584a687b>:0

  at Syncfusion.ListView.XForms.LinearLayout.SetRowHeight (Syncfusion.ListView.XForms.ListViewItemInfo itemInfo, System.Double itemSpacing) [0x000b0] in <9dbe1f5143c64addbe242929584a687b>:0

  at Syncfusion.ListView.XForms.LinearLayout.EnsureItems (Syncfusion.GridCommon.ScrollAxis.VisibleLinesCollection visibleLines) [0x00303] in <9dbe1f5143c64addbe242929584a687b>:0

  at Syncfusion.ListView.XForms.VisualContainer.EnsureItems () [0x0002c] in <9dbe1f5143c64addbe242929584a687b>:0

  at Syncfusion.ListView.XForms.VisualContainer.OnSizeAllocated (System.Double width, System.Double height) [0x00040] in <9dbe1f5143c64addbe242929584a687b>:0

  at Xamarin.Forms.VisualElement.SizeAllocated (System.Double width, System.Double height) [0x00000] in D:\a\1\s\Xamarin.Forms.Core\VisualElement.cs:798

  at Xamarin.Forms.VisualElement.SetSize (System.Double width, System.Double height) [0x00021] in D:\a\1\s\Xamarin.Forms.Core\VisualElement.cs:1027

  at Xamarin.Forms.VisualElement.set_Bounds (Xamarin.Forms.Rectangle value) [0x0005d] in D:\a\1\s\Xamarin.Forms.Core\VisualElement.cs:277

  at Xamarin.Forms.VisualElement.Layout (Xamarin.Forms.Rectangle bounds) [0x00000] in D:\a\1\s\Xamarin.Forms.Core\VisualElement.cs:684

  at Xamarin.Forms.Layout.LayoutChildIntoBoundingRegion (Xamarin.Forms.VisualElement child, Xamarin.Forms.Rectangle region) [0x001da] in D:\a\1\s\Xamarin.Forms.Core\Layout.cs:177

  at Syncfusion.ListView.XForms.ExtendedScrollView.LayoutChildren (System.Double x, System.Double y, System.Double width, System.Double height) [0x00058] in <9dbe1f5143c64addbe242929584a687b>:0

  at Xamarin.Forms.Layout.UpdateChildrenLayout () [0x0014b] in D:\a\1\s\Xamarin.Forms.Core\Layout.cs:263

  at Xamarin.Forms.Layout.OnSizeAllocated (System.Double width, System.Double height) [0x0000f] in D:\a\1\s\Xamarin.Forms.Core\Layout.cs:223

  at Xamarin.Forms.VisualElement.SizeAllocated (System.Double width, System.Double height) [0x00000] in D:\a\1\s\Xamarin.Forms.Core\VisualElement.cs:798

  at Xamarin.Forms.VisualElement.SetSize (System.Double width, System.Double height) [0x00021] in D:\a\1\s\Xamarin.Forms.Core\VisualElement.cs:1027

  at Xamarin.Forms.VisualElement.set_Bounds (Xamarin.Forms.Rectangle value) [0x0005d] in D:\a\1\s\Xamarin.Forms.Core\VisualElement.cs:277

  at Xamarin.Forms.VisualElement.Layout (Xamarin.Forms.Rectangle bounds) [0x00000] in D:\a\1\s\Xamarin.Forms.Core\VisualElement.cs:684

  at Syncfusion.ListView.XForms.SfListView.LayoutChildren (System.Double x, System.Double y, System.Double width, System.Double height) [0x0036f] in <9dbe1f5143c64addbe242929584a687b>:0



What can I do to correct this problem?

14 Replies

RS Rawoof Sharief Muthuja Sherif Syncfusion Team November 5, 2018 07:27 AM UTC

Hi Robert,   
   
Thanks for using Syncfusion products. 
 
We suspect that the reported issue “Application gets crashed when AutoFitMode set as Height” may occur due to SfListView assemblies might not be referred properly or SfListView renderer not initialized in your renderer projects. So, could you please check whether all the assemblies needed for proper functioning of SfListView control has been added as reference in your application? You can refer the below documentation to know about how to launch list view on each platform.        
        
       
Please let us know if you require further assistance.        
     
Regards,     
Rawoof M. 



SE Sergei February 6, 2019 03:40 PM UTC

I have the same issue for 16.4.0.52 package, but seems it's not related to auto-height. Using SfListview on Android, no initialization required on this platform, packages reinstalled.


GP Gnana Priya Namasivayam Syncfusion Team February 7, 2019 03:55 PM UTC

Hi Sergei, 
 
Yes. SfListView does not any initialization in android renderer but if any reference missed in renderer project you will get below exception or if you are getting any exception like `Target invocation exception` from ExtendedScrollviewRenderer then we would like to know that it occurs after updating Xamarin.Forms version to 3.5. SfListView will not reproduce any crash up to Xamarin.Forms version 3.4, so you can use SfListView with XF 3.4. We are currently validating this issue with XF 3.5 and will provide further details on February 8,2019 . We will appreciate your patience until then. 
 
If you are not getting any of these two exceptions, please share the call stack when the exception occurs in your sample. 
  
 
Regards,  
Gnana Priya N  
  




GP Gnana Priya Namasivayam Syncfusion Team February 9, 2019 05:55 AM UTC

Hi Sergei, 
 
 
We have confirmed that reported issue “Target invocation exception occurs when Xamarin forms version upgraded to 3.5” is a defect and logged a defect report for this bug. The fix will be included in our upcoming weekly nuget release on February 13, 2019. We will appreciate your patience until then. 
 
Regards, 
Gnana Priya N 



JN Jayaleshwari N Syncfusion Team February 14, 2019 06:28 AM UTC

Hi Sergei, 
  
We are glad to announce that our latest weekly NuGet package update version 16.4.0.53 has been rolled out with the fix included for the issue “Target invocation exception occurs when Xamarin forms version upgraded to 3.5” and is available for download. 
 
We thank you for your support and appreciate your patience in waiting for this update. Please get in touch with us if you would require any further assistance. 
  
Regards, 
Jayaleshwari N 



BE Bertrand February 14, 2019 10:22 AM UTC

Back to the original issue discussed here (Null Reference Exception when autosizing ListView).
I'm now facing the same issue using Visual Studio 2019 16.0.0 preview 3.0.
No problem when using Visual Studio 2017 15.9.7.

Please advise.
Bertrand



JN Jayaleshwari N Syncfusion Team February 15, 2019 12:15 PM UTC

Hi Bertrand,  
   
We would like to know that if Null reference exception occurs from GetAutoFitItemSize() then it occurs mostly because of missing reference in renderer projects. Please ensure whether your project has referred all assemblies and initialized properly. Currently we are not using VS 2019 preview. We will check the reported issue after upgrading Vs 2019 preview version and provide details on or before February 19, 2019. We will appreciate your patience until then.   
  
Regards,  
Jayaleshwari N 



BE Bertrand February 15, 2019 05:07 PM UTC

I'm pretty sure references and inits are ok.
Again, it works perfectly with VS2017, the issue is only with VS2019.
I'll wait for you to do the test, let me know if it works or not.
Thanks
Bertrand


SR Sangeetha Raju Syncfusion Team February 18, 2019 12:53 PM UTC

Hi Bertrand, 
 
As mentioned in our earlier update, we will check the reported issue after upgrading Vs 2019 preview version and provide details on February 19, 2019.We appreciate your patience until then. 
 
Regards, 
Sangeetha Raju 



GP Gnana Priya Namasivayam Syncfusion Team February 20, 2019 01:08 PM UTC

Hi Bertrand, 
 
Sorry for the delay. 
 
We would like to know that reported crash occurs after updating on release mode in UWP alone. To overcome this null reference exception from GetAutoSize method you have to register assemblies as mentioned in following link. 
 
 
Meanwhile, if you are getting any crash in android (release mode) from native element with Xamarin Forms 3.5 then you can follow the attached bug reported logged to xamarin team from below. 
 
 
 
Please let us know if you are facing issue other than these cases. 
 
 
Regards, 
Gnana Priya N 



MA Manuel July 14, 2022 08:52 PM UTC

Hey SyncFusion Team,


I need to bring this topic up because I am facing the exact same problem but on android platform. As soon as I use AutoFitMode with either DynamicHeight or Height, my android app crashes with 

java.lang.NullPointerException: Attempt to invoke interface method 'int java.lang.CharSequence.length()' on a null object reference 

when my underlaying observable collection is changing. This does not happen on UWP.

Are there still any known issues with that functionality?

Xamarin.Forms 5.0.0.2478

Synfusion.Xamarin.SfListView 20.2.0.38

Microsoft Visual Studio Community 2022 17.2.6



SY Suthi Yuvaraj Syncfusion Team July 15, 2022 02:00 PM UTC

Hi Manuel,

We have checked the reported query "Null Reference Exception when autosizing ListView" from our end. We would like to inform you that changing observable collection at runtime with AutoFitMode as height or Dynamic height works fine without any crash. We have attached a workable sample for your reference.

Also, please modify the sample to the reported scenario and share the below details, which would be helpful for us to check on it and provide you with the solution as soon as possible.

  1. SfListView code snippets
  2. Replication Video
  3. Syncfusion and Xamarin.Forms versions

Regards,

Suthi Yuvaraj



Attachment: GettingStarted_c48a3d56.zip


MA Manuel July 20, 2022 08:48 AM UTC

Here is a screencast where hte problem is dispalyed, with fixed height at beginning everything works as expected. When switching to AutoFitMode the app starts crashing randomly when the pages are changed with the flyoutmenu. Debugging shows this occuring when manipulating the ItemSources content. 


Xamarin.Forms 5.0.0.2478

Synfusion.Xamarin.SfListView 20.2.0.38


I would upload the whole project but it is way to big for it and needs expertise to get everything set up - sry.

Here is the code snippet.

<sf:SfListView x:Name="RepeaterItems"
                       ItemsSource="{x:Binding BuildingPartItems}"
                       ItemTemplate="{x:StaticResource ActionableItemTemplateSelector}"
                       AutoFitMode="DynamicHeight"
                       AllowSwiping="{x:Binding EditModeEnabled}"
                       SelectionMode="None"
                       DragStartMode="OnDragIndicator">
                    <sf:SfListView.DragDropController>
                        <sf:DragDropController UpdateSource="True" />
                    </sf:SfListView.DragDropController>
                    <sf:SfListView.RightSwipeTemplate>
                        <DataTemplate >
                            <Grid>
                                <Grid BackgroundColor="{StaticResource KNXBlue}" HorizontalOptions="Fill" VerticalOptions="Fill" Grid.Column="0">
                                    <Grid VerticalOptions="Center" HorizontalOptions="Center">
                                        <Grid.GestureRecognizers>
                                            <TapGestureRecognizer Command="{x:Binding BindingContext.DisplayEditPopupCommand, Source={x:Reference RepeaterItems}}" CommandParameter="{x:Binding .}"/>
                                            <TapGestureRecognizer Command="{x:Binding BindingContext.ResetSwipeCommand, Source={x:Reference RepeaterItems}}" CommandParameter="{x:Reference RepeaterItems}"/>
                                        </Grid.GestureRecognizers>
                                        <Image Grid.Column="0"
                                    Grid.Row="0"
                                    BackgroundColor="Transparent"
                                    HeightRequest="20"
                                    WidthRequest="20">
                                            <Image.Source>
                                                <FontImageSource FontFamily="MaterialDesignIcons"
                                            Glyph="{x:Static fonts:MaterialDesignIcons.Pencil}"
                                            Color="{AppThemeBinding Dark={StaticResource DarkFontColor}, Light={StaticResource LightFontColor}}" />
                                            </Image.Source>
                                        </Image>
                                    </Grid>
                                </Grid>
                            </Grid>
                        </DataTemplate>
                    </sf:SfListView.RightSwipeTemplate>
                    <sf:SfListView.LeftSwipeTemplate>
                        <DataTemplate >
                            <Grid>
                                <Grid BackgroundColor="#DC595F" HorizontalOptions="Fill" VerticalOptions="Fill" Grid.Column="0">
                                    <Grid VerticalOptions="Center" HorizontalOptions="Center">
                                        <Grid.GestureRecognizers>
                                            <TapGestureRecognizer Command="{x:Binding BindingContext.DeleteItemCommand, Source={x:Reference RepeaterItems}}" CommandParameter="{x:Binding .}"/>
                                            <TapGestureRecognizer Command="{x:Binding BindingContext.ResetSwipeCommand, Source={x:Reference RepeaterItems}}" CommandParameter="{x:Reference RepeaterItems}"/>
                                        </Grid.GestureRecognizers>
                                        <Image Grid.Column="0"
                                    Grid.Row="0"
                                    BackgroundColor="Transparent"
                                    HeightRequest="20"
                                    WidthRequest="20">
                                            <Image.Source>
                                                <FontImageSource FontFamily="MaterialDesignIcons"
                                            Glyph="{x:Static fonts:MaterialDesignIcons.Delete}"
                                            Color="{AppThemeBinding Dark={StaticResource DarkFontColor}, Light={StaticResource LightFontColor}}" />
                                            </Image.Source>
                                        </Image>
                                    </Grid>
                                </Grid>
                            </Grid>
                        </DataTemplate>
                    </sf:SfListView.LeftSwipeTemplate>
                </sf:SfListView>



Attachment: Scrrencast_af2c53f6.zip


SY Suthi Yuvaraj Syncfusion Team July 21, 2022 02:22 PM UTC

Hi Manuel,


We have checked the reported query from our end. Flyout Page with SfListView , AutoFitMode as height or Dynamic height works fine without any crash. We have attached a workable sample for your reference.


Also, please modify the sample to the reported scenario and share the below details, which would be helpful for us to check on it and provide you with the solution as soon as possible.

  1. SfListView code snippets
  2. Replication Video
  3. Tested Device Details
  4. Syncfusion and Xamarin.Forms versions
  5. StackTrace details


Regards,

Suthi Yuvaraj


Attachment: FlyoutSample_ea0ef3b9.zip

Loader.
Up arrow icon