3

So I am writing a Xamarin application and I have built a CustomRenderer

now my method:

horizontalScrollView.ScrollTo(this.Width, 0);

keeps throwing an exception:

System.ArgumentException: 'jobject' must not be IntPtr.Zero. 10-21 12:10:41.898 E/AndroidRuntime(15053): Parameter name: jobject 10-21 12:10:41.898 E/AndroidRuntime(15053): at Android.Runtime.JNIEnv.CallIntMethod (intptr,intptr) [0x00010] in /Users/builder/data/lanes/2185/53fce373/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:378 10-21 12:10:41.898 E/AndroidRuntime(15053): at Android.Views.View.get_Width ()

So looking at the exception and from my breakpoints I find that the problem is that when I hover over this.Width I get 'jobject' must not be IntPtr.Zero instead of an integer so I try to defensively code against this and do:

if (this.Width != IntPtr.Zero)
{
   horizontalScrollView.ScrollTo(this.Width, 0);
}

but this doesnt compile and gives me the error:

Operator '!=' cannot be applied to operands of type 'int' and 'IntPtr'

So my questions is. Is there a way to check whether a value type is equal to IntPtr.Zero?

SushiHangover
  • 73,120
  • 10
  • 106
  • 165
JKennedy
  • 18,150
  • 17
  • 114
  • 198

2 Answers2

2

Since you are working on a custom renderer I assume you are using Xamarin.Forms. You can't check for IntPtr.Zero here. The IntPtr is set internally by Xamarin.Forms. You have usually no access to the wrong pointer. So you can't see it's there but you receive an error when you access the Width property because it works internally with that pointer.

There are two possible causes for this error:

  1. You didn't initialize the control properly. So it contains null pointers.
  2. There is a bug in Xamarin.Forms. Test your code against other versions of Xamarin.Forms. If the error doesn't occur in a different version or you believe it's a bug then file it here.
Wosi
  • 41,986
  • 17
  • 75
  • 82
0

Maybe you have to check against the jobject and not against the property-value 'width'.

creg
  • 154
  • 3
  • 13