Bug 38873 - NullReferenceException when setting a Padding value on a StackLayout inside a ListView
Summary: NullReferenceException when setting a Padding value on a StackLayout inside a...
Status: CONFIRMED
Alias: None
Product: Forms
Classification: Xamarin
Component: Windows ()
Version: unspecified
Hardware: PC Windows
: Normal major
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2016-02-18 12:05 UTC by David Britch
Modified: 2017-12-12 20:40 UTC (History)
6 users (show)

Tags: ac
Is this bug a regression?: ---
Last known good build:


Attachments
Sample (992.54 KB, application/zip)
2016-02-18 12:05 UTC, David Britch
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report for Bug 38873 on Developer Community or GitHub if you have new information to add and do not yet see a matching new report.

If the latest results still closely match this report, you can use the original description:

  • Export the original title and description: Developer Community HTML or GitHub Markdown
  • Copy the title and description into the new report. Adjust them to be up-to-date if needed.
  • Add your new information.

In special cases on GitHub you might also want the comments: GitHub Markdown with public comments

Related Links:
Status:
CONFIRMED

Description David Britch 2016-02-18 12:05:30 UTC
Created attachment 15086 [details]
Sample

I'm getting a NullReferenceException from the following XAML in a Forms-WinRT project (2.1.0.6513-pre4).

		<ListView x:Name="listView" HasUnevenRows="true">
			<ListView.ItemTemplate>
				<DataTemplate>
					<ViewCell>
						<StackLayout Padding="15,5,5,5" VerticalOptions="StartAndExpand">
							<Label Text="{Binding}" />
						</StackLayout>
					</ViewCell>
				</DataTemplate>
			</ListView.ItemTemplate>
		</ListView>
		
System.NullReferenceException: Object reference not set to an instance of an object.\r\n   at Xamarin.Forms.StackLayout.AlignOffAxis(LayoutInformation layout, StackOrientation orientation, Double widthConstraint, Double heightConstraint)\r\n   at Xamarin.Forms.StackLayout.LayoutChildren(Double x, Double y, Double width, Double height)\r\n   at Xamarin.Forms.Layout.UpdateChildrenLayout()\r\n   at Xamarin.Forms.Layout.OnSizeAllocated(Double width, Double height)\r\n   at Xamarin.Forms.VisualElement.SizeAllocated(Double

If I remove the Padding from the StackLayout the exception doesn't occur:

		<ListView x:Name="listView" HasUnevenRows="true">
			<ListView.ItemTemplate>
				<DataTemplate>
					<ViewCell>
						<StackLayout VerticalOptions="StartAndExpand">
							<Label Text="{Binding}" />
						</StackLayout>
					</ViewCell>
				</DataTemplate>
			</ListView.ItemTemplate>
		</ListView>

Repro case attached.
		
I also tried downgrading the project to 2.0.1.6505 and get a MethodAccessException instead:

Attempt by method 'UnevenListCells.HomePage.InitializeComponent()' to access method 'Xamarin.Forms.DataTemplate..ctor()' failed.
Comment 1 Rod 2016-09-14 06:24:43 UTC
I have this problem - and removing all 27 padding statements from my XAML removes the problem, which of course screws up my layout. Is there are patch for this bug?

MY STACKTRACE:
Message:	Object reference not set to an instance of an object
Source:		Xamarin.Forms.Core

StackTrace:
  at Xamarin.Forms.StackLayout.AlignOffAxis (Xamarin.Forms.LayoutInformation layout, StackOrientation orientation, Double widthConstraint, Double heightConstraint) [0x0004a] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\StackLayout.cs:85 
  at Xamarin.Forms.StackLayout.LayoutChildren (Double x, Double y, Double width, Double height) [0x00037] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\StackLayout.cs:39 
  at Xamarin.Forms.Layout.UpdateChildrenLayout () [0x000c7] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\Layout.cs:229 
  at Xamarin.Forms.Layout.OnIsVisibleChanged (Boolean oldValue, Boolean newValue) [0x00029] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\Layout.cs:346 
  at Xamarin.Forms.VisualElement+<>c.<.cctor>b__212_2 (Xamarin.Forms.BindableObject bindable, System.Object oldvalue, System.Object newvalue) [0x00000] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\VisualElement.cs:52 
  at Xamarin.Forms.BindableObject.SetValueActual (Xamarin.Forms.BindableProperty property, Xamarin.Forms.BindablePropertyContext context, System.Object value, Boolean currentlyApplying, SetValueFlags attributes, Boolean silent) [0x00108] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\BindableObject.cs:584 
  at Xamarin.Forms.BindableObject.SetValueCore (Xamarin.Forms.BindableProperty property, System.Object value, SetValueFlags attributes, SetValuePrivateFlags privateAttributes) [0x0014b] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\BindableObject.cs:378 
  at Xamarin.Forms.BindingExpression.ApplyCore (System.Object sourceObject, Xamarin.Forms.BindableObject target, Xamarin.Forms.BindableProperty property, Boolean fromTarget) [0x0020c] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\BindingExpression.cs:177 
  at Xamarin.Forms.BindingExpression.Apply (Boolean fromTarget) [0x0003e] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\BindingExpression.cs:55 
  at Xamarin.Forms.BindingExpression+BindingExpressionPart.<PropertyChanged>b__44_0 () [0x00000] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\BindingExpression.cs:475 
  at Foundation.NSAsyncActionDispatcher.Apply () [0x00000] in /Users/builder/data/lanes/3539/f37444ae/source/maccore/src/Foundation/NSAction.cs:163 
  at (wrapper managed-to-native) UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
  at UIKit.UIApplication.Main (System.String[] args, IntPtr principal, IntPtr delegate) [0x00005] in /Users/builder/data/lanes/3539/f37444ae/source/maccore/src/UIKit/UIApplication.cs:79 
  at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0001c] in /Users/builder/data/lanes/3539/f37444ae/source/maccore/src/UIKit/UIApplication.cs:62 
  at ClimatarianChallenge.iOS.Application.Main (System.String[] args) [0x00000] in /Users/rodhemphill/Documents/Business/Audio Puppy/Development/Climatarian/iOS/Main.cs:17
Comment 2 Nicholas Bauer 2017-09-21 08:12:06 UTC
Just found possibly the same bug with different cause. My layout was working perfectly with the padding, until it didn't. Working on narrowing down a sample project. My StackLayout isn't in a ListView, but I get a NullReferenceException in AlignOffAxis() just like these reports.

UWP, recent Xamarin.Forms stable version. More details to come.
Comment 3 Nicholas Bauer 2017-09-21 23:35:05 UTC
The crash is happening with a very particular set of circumstances.

MainPage.xaml:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MyApp.MainPage">
    <AbsoluteLayout>
        <StackLayout x:Name="buggy"
                     HorizontalOptions="Center"
                     VerticalOptions="Center"
                     IsVisible="false"
                     Padding="5">
            <Label />
        </StackLayout>

        <Button Text="Crash!"
                Clicked="Crash_Clicked"
                AbsoluteLayout.LayoutFlags="All"
                AbsoluteLayout.LayoutBounds="0.5, 0.5, 0.5, 0.5"/>
    </AbsoluteLayout>
</ContentPage>


MainPage.xaml.cs:
using System;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace MyApp
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class MainPage : ContentPage
    {
        public MainPage() => InitializeComponent();

        void Crash_Clicked(object sender, EventArgs e) => buggy.IsVisible = !buggy.IsVisible;
    }
}


It *only* happens when switching from IsVisible = false to IsVisible = true. If it starts out visible, no crash. If you change the initial state to true, it won't crash until the second button press.

If I remove any one of the ___Options, or the padding, or the child of the StackLayout, no crash occurs.

If I change the AbsoluteLayout to RelativeLayout, crash occurs; to Grid, or StackLayout, no crash occurs.
Comment 4 Nicholas Bauer 2017-09-22 00:37:17 UTC
Oh, another detail - if the Label's text is set => No crash. If it is NOT set, no crash.
Comment 5 Nicholas Bauer 2017-09-22 00:38:04 UTC
Correction:
Text set = no crash
Text empty = crash
Comment 6 FieldstrikeMobile 2017-10-12 16:26:15 UTC
I am also seeing this on Android on some devices
Comment 7 Ben Askren 2017-12-12 20:40:00 UTC
Status on a fix?  Anyone have a work around?