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

ListView System.NullReferenceException #5700

Closed
kericbowman opened this issue Mar 26, 2019 · 10 comments · Fixed by #5756
Closed

ListView System.NullReferenceException #5700

kericbowman opened this issue Mar 26, 2019 · 10 comments · Fixed by #5756
Labels
a/listview Problems with the ListView/TableView p/Android s/needs-repro ❔ This reported issue doesn't include a sample project reproducing the issue. Please provide one. s/unverified New report that has yet to be verified t/bug 🐛
Projects

Comments

@kericbowman
Copy link
Contributor

Description

This only occurs on Android

I have a listview on a page in my app that is producing a System.NullReferenceException. This happens when its page is not at the top of the navigation stack.

Stack trace:

[MonoDroid] UNHANDLED EXCEPTION:
[MonoDroid] System.NullReferenceException: Object reference not set to an instance of an object.
[MonoDroid] at Xamarin.Forms.Platform.Android.ListViewRenderer+Container.OnMeasure (System.Int32 widthMeasureSpec, System.Int32 heightMeasureSpec) [0x00032] in D:\a\1\s\Xamarin.Forms.Platform.Android\Renderers\ListViewRenderer.cs:513
[MonoDroid] at Android.Views.View.n_OnMeasure_II (System.IntPtr jnienv, System.IntPtr native__this, System.Int32 widthMeasureSpec, System.Int32 heightMeasureSpec) [0x00009] in <3faca578033c421aa590cc3addd3b6d8>:0
[MonoDroid] at (wrapper dynamic-method) System.Object.29(intptr,intptr,int,int)

Steps to Reproduce

Narrowing this down to a repro; just wanted to get it up there.

Expected Behavior

I did not expect a null reference exception

Actual Behavior

a null reference exception

Basic Information

  • Version with issue:
  • Last known good version:
  • IDE:
  • Platform Target Frameworks:
    • Android: not sure how to answer this? I support multiple platforms and it seems to happen on all of them

Reproduction Link

Working on it

@kingces95
Copy link
Contributor

Please let us know when you have a zipped reproduction. Thanks.

@kingces95 kingces95 moved this from New to Needs Info in Triage Mar 26, 2019
@samhouts samhouts moved this from Needs Info to New in Triage Mar 26, 2019
@kericbowman
Copy link
Contributor Author

I'm having a hard time reproducing this; is there a way I could debug the xamarin.forms source code? Is there a verbose debugging option I can turn on to get more info?

@kericbowman
Copy link
Contributor Author

kericbowman commented Mar 29, 2019

Haven't been able to reproduce so I'm looking at the source. this is the function that crashes:

protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
if (_child == null)
{
	SetMeasuredDimension(0, 0);
	return;
}

VisualElement element = _child.Element;

Context ctx = Context;

var width = (int)ctx.FromPixels(MeasureSpecFactory.GetSize(widthMeasureSpec));

SizeRequest request = _child.Element.Measure(width, double.PositiveInfinity, MeasureFlags.IncludeMargins);
Xamarin.Forms.Layout.LayoutChildIntoBoundingRegion(_child.Element, new Rectangle(0, 0, width, request.Request.Height));

int widthSpec = MeasureSpecFactory.MakeMeasureSpec((int)ctx.ToPixels(width), MeasureSpecMode.Exactly);
int heightSpec = MeasureSpecFactory.MakeMeasureSpec((int)ctx.ToPixels(request.Request.Height), MeasureSpecMode.Exactly);

_child.View.Measure(widthMeasureSpec, heightMeasureSpec);
SetMeasuredDimension(widthSpec, heightSpec);
}

specifically, this line throws the nullreference exception:

SizeRequest request = _child.Element.Measure(width, double.PositiveInfinity, MeasureFlags.IncludeMargins);

Since _child has a null check above, and everything else except "Element" is non-nullable on that line isn't it safe to say that "Element" is null?

I'll make the edit and start the pull request. In the mean time; any ideas why "element" could be null and how I could get a work around going on my end?

@kingces95
Copy link
Contributor

The project is open source and has instructions per platform for how to build it. There is an unofficial bat file .create-nuget that you could use to build the nuget or just create ur own unit test. If u use the bat file, set nuget_dir= path to nuget.exe.

@kingces95 kingces95 added the s/needs-repro ❔ This reported issue doesn't include a sample project reproducing the issue. Please provide one. label Mar 30, 2019
@kingces95 kingces95 moved this from New to Needs Info in Triage Mar 30, 2019
@kericbowman
Copy link
Contributor Author

I tried following the build tutorial but it's for windows and I'm on a mac. Is there a tutorial for building the nuget package with visual studio for mac?

@kericbowman
Copy link
Contributor Author

upgraded to the latest 4.0 release and the bug seems to have gone away.

@kericbowman
Copy link
Contributor Author

And it's back, I'll keep working on a repro

@samhouts samhouts added a/listview Problems with the ListView/TableView p/Android labels Apr 5, 2019
@samhouts
Copy link
Member

samhouts commented Apr 5, 2019

Thanks, we'll keep watching for it. :)

@kericbowman
Copy link
Contributor Author

kericbowman commented Apr 8, 2019

Still can't reproduce but I am narrowing it down.

This happens if the OnMeasure method is called on a listview on a page that has been removed from the navigation stack (not popped, removed). I'm not sure why that method is called on a page that is no longer on the navigation stack. I even set the page to null after popping it and it the onMeasure method is still called.

@kericbowman
Copy link
Contributor Author

Changed from Navigation.remove to popping the page and the same problem persists.

@samhouts samhouts added the s/unverified New report that has yet to be verified label Apr 9, 2019
@samhouts samhouts added this to In Progress in v4.1.0 Apr 15, 2019
@samhouts samhouts removed this from Needs Info in Triage Apr 15, 2019
samhouts pushed a commit that referenced this issue Apr 29, 2019
* Fixes _child.Element null reference exception

* Update ListViewRenderer.cs

fixes #5700
@samhouts samhouts moved this from In Progress to Done in v4.1.0 Apr 30, 2019
@samhouts samhouts added this to In Progress in v4.2.0 May 29, 2019
@samhouts samhouts removed this from Done in v4.1.0 May 29, 2019
@samhouts samhouts moved this from In Progress to Done in v4.2.0 May 29, 2019
@samhouts samhouts removed this from Done in v4.2.0 Jun 4, 2019
@samhouts samhouts added this to In Progress in v4.1.0 Jun 4, 2019
@samhouts samhouts moved this from In Progress to Done in v4.1.0 Jun 5, 2019
AxelUser pushed a commit to AxelUser/Xamarin.Forms that referenced this issue Jun 15, 2019
* Fixes _child.Element null reference exception

* Update ListViewRenderer.cs

fixes xamarin#5700
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a/listview Problems with the ListView/TableView p/Android s/needs-repro ❔ This reported issue doesn't include a sample project reproducing the issue. Please provide one. s/unverified New report that has yet to be verified t/bug 🐛
Projects
No open projects
v4.1.0
  
Done
Development

Successfully merging a pull request may close this issue.

3 participants