-
Notifications
You must be signed in to change notification settings - Fork 1.9k
System.NullReferenceException: Object reference not set to an instance of an object #4581
Comments
Do you have a sample project that demonstrates the issue? Thanks! |
I haven't a repro project because I can't never reproduce this error. I just get data collected with App Center |
I've created a PR to solve this problem: |
@transis2 I wonder if this is caused by updating the property bound to the labels text off the UI Thread Do you know what view/vm change your stack trace correlates to? Can you try to ensure that it is only updated on the UI Thread to see if that resolves the issue? |
This error happens during an thread that is updating the screen and the user press back button or close the app System.Threading.Tasks.Task.Run(() => { DoSomeJobs(); });
private void DoSomeJobs()
{
//MORE CODE HERE....
Device.BeginInvokeOnMainThread(() =>
{
LABEL.Text = mensagem;
});
} |
@transis2 so in theory if you added a check above private void DoSomeJobs()
{
//MORE CODE HERE....
Device.BeginInvokeOnMainThread(() =>
{
if(Label.BindingContext != null && UserHasntNavigatedBack)
LABEL.Text = mensagem;
});
} |
Yes! I've already did this test but not work. Not even inserting a Try Catch. |
We have the same errors here. They are seldom and sporadic. BindableObject.GetValue (Xamarin.Forms.BindableProperty property) |
@transis2 @berndu Ok! Can you list the controls that you use on the affected pages? Let's see if we can narrow it down. Thanks! |
@samhouts we have tried another way to get rid of the problem and have seen we used a Task.Run in our navigation service that creates the view model and the view (page). After we made sure the creation of the page is in the main thread/ui thread we haven't seen these crashes any more (runs now for one week). A proposal would be to add a "must run on main thread" in InitilizeComponent somehow. |
I've used only native controls This is the XAML code of the UI
|
Interesting. I'll tag this for further investigation. Thanks! |
Look at this similar thread and how they use the same approach (!= null) to fix: https://github.com/xamarin/Xamarin.Forms/pull/5423/files Thanks |
Any news on this? I got a similar issue on a boolean target iOS and Android.
Let me know, |
Hello Guys, I just found one case where I had this issue on a Xaml Binding Date.
I manage to resolve this by providing a FallBackValue like this
Hope it will help someone |
This is my suggestion to fix the bug described bellow
\Xamarin.Forms.Core\VisualElement.cs
Actual Code
[TypeConverter(typeof(VisibilityConverter))]
public bool IsVisible
{
get { return (bool)GetValue(IsVisibleProperty); }
set { SetValue(IsVisibleProperty, value); }
}
New Code
[TypeConverter(typeof(VisibilityConverter))]
public bool IsVisible
{
get { return IsVisibleProperty != null ? (bool)GetValue(IsVisibleProperty) : false; }
set { SetValue(IsVisibleProperty, value); }
}
I've updated the code above to prevent the error below.
Xamarin Exception Stack:
System.NullReferenceException: Object reference not set to an instance of an object
at Xamarin.Forms.BindableObject.GetValue (Xamarin.Forms.BindableProperty property) [0x00016] in <2213e65041254f04b27ae6e57d256a86>:0
at Xamarin.Forms.VisualElement.get_IsVisible () [0x00000] in <2213e65041254f04b27ae6e57d256a86>:0
at Xamarin.Forms.Layout.OnChildMeasureInvalidated (Xamarin.Forms.VisualElement child, Xamarin.Forms.Internals.InvalidationTrigger trigger) [0x00025] in <2213e65041254f04b27ae6e57d256a86>:0
at Xamarin.Forms.Layout.OnChildMeasureInvalidated (System.Object sender, System.EventArgs e) [0x00013] in <2213e65041254f04b27ae6e57d256a86>:0
at Xamarin.Forms.VisualElement.InvalidateMeasureInternal (Xamarin.Forms.Internals.InvalidationTrigger trigger) [0x0001d] in <2213e65041254f04b27ae6e57d256a86>:0
at Xamarin.Forms.Label.OnTextPropertyChanged (Xamarin.Forms.BindableObject bindable, System.Object oldvalue, System.Object newvalue) [0x0002b] in <2213e65041254f04b27ae6e57d256a86>:0
at Xamarin.Forms.BindableObject.SetValueActual (Xamarin.Forms.BindableProperty property, Xamarin.Forms.BindableObject+BindablePropertyContext context, System.Object value, System.Boolean currentlyApplying, Xamarin.Forms.Internals.SetValueFlags attributes, System.Boolean silent) [0x0012a] in <2213e65041254f04b27ae6e57d256a86>:0
at Xamarin.Forms.BindableObject.SetValueCore (Xamarin.Forms.BindableProperty property, System.Object value, Xamarin.Forms.Internals.SetValueFlags attributes, Xamarin.Forms.BindableObject+SetValuePrivateFlags privateAttributes) [0x0015b] in <2213e65041254f04b27ae6e57d256a86>:0
at Xamarin.Forms.BindableObject.SetValue (Xamarin.Forms.BindableProperty property, System.Object value, System.Boolean fromStyle, System.Boolean checkAccess) [0x0003d] in <2213e65041254f04b27ae6e57d256a86>:0
at Xamarin.Forms.BindableObject.SetValue (Xamarin.Forms.BindableProperty property, System.Object value) [0x00000] in <2213e65041254f04b27ae6e57d256a86>:0
at Xamarin.Forms.Label.set_Text (System.String value) [0x00000] in <2213e65041254f04b27ae6e57d256a86>:0
at TransisMobileX.View.Conexao.Conexao+<>c__DisplayClass47_0.b__0 () [0x00000] in :0
at Java.Lang.Thread+RunnableImplementor.Run () [0x00008] in <616e37f9ed964e569785d5d641b4307c>:0
at Java.Lang.IRunnableInvoker.n_Run (System.IntPtr jnienv, System.IntPtr native__this) [0x00009] in <616e37f9ed964e569785d5d641b4307c>:0
at (wrapper dynamic-method) System.Object.24(intptr,intptr)
The text was updated successfully, but these errors were encountered: