diff --git a/src/Java.Interop/Java.Interop/JniEnvironment.Types.cs b/src/Java.Interop/Java.Interop/JniEnvironment.Types.cs index af0ca4ff1..a50f0b19a 100644 --- a/src/Java.Interop/Java.Interop/JniEnvironment.Types.cs +++ b/src/Java.Interop/Java.Interop/JniEnvironment.Types.cs @@ -200,9 +200,15 @@ public static void RegisterNatives (JniObjectReference type, JniNativeMethodRegi public static void RegisterNatives (JniObjectReference type, JniNativeMethodRegistration [] methods, int numMethods) { + if ((numMethods < 0) || + (numMethods > (methods?.Length ?? 0))) { + throw new ArgumentOutOfRangeException (nameof (numMethods), numMethods, + $"`numMethods` must be between 0 and `methods.Length` ({methods?.Length ?? 0})!"); + } #if DEBUG && NETCOREAPP - foreach (var m in methods) { - if (m.Marshaler.GetType ().GenericTypeArguments.Length != 0) { + for (int i = 0; methods != null && i < numMethods; ++i) { + var m = methods [i]; + if (m.Marshaler != null && m.Marshaler.GetType ().GenericTypeArguments.Length != 0) { var method = m.Marshaler.Method; Debug.WriteLine ($"JNIEnv::RegisterNatives() given a generic delegate type `{m.Marshaler.GetType()}`. .NET Core doesn't like this."); Debug.WriteLine ($" Java: {m.Name}{m.Signature}");