Bug 15973 - System.NullReferenceException occurring only in iOS7
Summary: System.NullReferenceException occurring only in iOS7
Status: RESOLVED FEATURE
Alias: None
Product: iOS
Classification: Xamarin
Component: General ()
Version: 7.0.4.x
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2013-11-06 14:13 UTC by Allie Miller
Modified: 2013-11-08 14:23 UTC (History)
3 users (show)

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

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 on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FEATURE

Description Allie Miller 2013-11-06 14:13:23 UTC
When deploy the attached iPad project in debug mode targeting iOS 6, the project runs fine. However, when targeting iOS 7, the following error occurs:

"System.NullReferenceException: Object reference not set to an instance of an object
  at zeltiq_crm.zedCRM_MobileViewController.StartLoad (MonoTouch.UIKit.UIWebView webview, MonoTouch.Foundation.NSUrlRequest request, UIWebViewNavigationType navigationType) [0x00007] in /Users/XXXXX/Downloads/zedCRMMobile/zedCRM_MobileViewController.cs:96
  at MonoTouch.UIKit.UIWebView+_UIWebViewDelegate.ShouldStartLoad (MonoTouch.UIKit.UIWebView webView, MonoTouch.Foundation.NSUrlRequest request, UIWebViewNavigationType navigationType) [0x0000d] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIWebView.g.cs:739
  at at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
  at MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0004c] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:38
  at zeltiq_crm.Application.Main (System.String[] args) [0x00008] in /Users/XXX/Downloads/zedCRMMobile/Main.cs:17

Crash logs are also attached to this bug report.


This occurs on the alpha, beta and stable version of the software. 
=== Xamarin Studio ===

Version 4.1.13 (build 17)
Installation UUID: f6a3c61f-a7e2-4740-ba09-86314760e340
Runtime:
	Mono 3.2.4 ((no/294f999)
	GTK+ 2.24.20 theme: Raleigh
	GTK# (2.12.0.0)
	Package version: 302040000

=== Apple Developer Tools ===

Xcode 5.0.1 (3335.23)
Build 5A2053

=== Xamarin.Mac ===

Xamarin.Mac: 1.6.19

=== Xamarin.Android ===

Version: 4.10.1 (Business Edition)
Android SDK: /Users/XXX/Library/Developer/Xamarin/android-sdk-mac_x86
	Supported Android versions:
		2.1   (API level 7)
		2.2   (API level 8)
		2.3   (API level 10)
		3.1   (API level 12)
		3.2   (API level 13)
		4.0   (API level 14)
		4.0.3 (API level 15)
		4.1   (API level 16)
		4.2   (API level 17)
		4.3   (API level 18)
Java SDK: /usr
java version "1.6.0_65"
Java(TM) SE Runtime Environment (build 1.6.0_65-b14-462-11M4609)
Java HotSpot(TM) 64-Bit Server VM (build 20.65-b04-462, mixed mode)

=== Xamarin.iOS ===

Version: 7.0.4.171 (Business Edition)
Hash: 2593576
Branch: 
Build date: 2013-31-10 21:12:14-0400

=== Build Information ===

Release ID: 401130017
Git revision: 9f5e11a6c7592b94b3cbb3582fcf0f9d774b9710
Build date: 2013-11-01 20:33:42+0000
Xamarin addins: 98c72290fde44b199ca0344a8c52ab5ab9dbf56d

=== Operating System ===

Mac OS X 10.9.0
Darwin XXX-MacBook-Air.local 13.0.0 Darwin Kernel Version 13.0.0
    Thu Sep 19 22:22:27 PDT 2013
    root:xnu-2422.1.72~6/RELEASE_X86_64 x86_64
Comment 3 Sebastien Pouliot 2013-11-06 16:48:47 UTC
This is not related to the UIWebView.

	string url = loginUrl;
	var req = new NSUrlRequest(new NSUrl (url));

req.Url is null - even before calling the UIWebView.
Comment 4 Sebastien Pouliot 2013-11-06 16:51:55 UTC
Changing the loginUrl to:

	string loginUrl = ServerURL; // + String.Format(AppSettings.URL_HOLDER,Username,Password);

works. So the provided URL is not accepted by iOS7, that's likely a new validation against invalid URL.
Comment 5 Sebastien Pouliot 2013-11-06 17:00:04 UTC
var url = new NSUrl (loginUrl);

returns an empty (handle == 0x0) NSUrl instance.
Comment 6 Sebastien Pouliot 2013-11-06 17:06:10 UTC
Encode your '-' characters with "%96" and iOS7 will be happy with your URL.
Comment 8 Curtis Traverse 2013-11-08 07:41:57 UTC
I changed our URL to the below string as instructed:

public const string URL_HOLDER = @"/login/MobileLogin.aspx?username={0}&password={1}&mobilekey=35F60CAA%962D5E%9640EA%96B5BD%696C0B7D2D58281
";


I still get the exact same error with the null references so this work around did not resolve the issue.


Curtis
Comment 9 Curtis Traverse 2013-11-08 07:46:54 UTC
Sorry there was a typo in that comment I posted, this is the URL I tried and it fails with the same error.

public const string URL_HOLDER =
@"/login/MobileLogin.aspx?username={0}&password={1}&mobilekey=35F60CAA%962D5E%9640EA%96B5BD%96C0B7D2D58281
";
Comment 10 Sebastien Pouliot 2013-11-08 08:20:00 UTC
What I did above was not a workaround :-) as the solution is not to change the URL_HOLDER but to produce valid URL strings (which the current URL_HOLDER constant made impossible).

So the point is that the string given to `new NSUrl(string)` must be a valid URL representation or iOS7 won't give you a valid `NSUrl` instance (which will be null inside an NSUrlRequest).

Now the URL string you are using is composed from the URL_HOLDER and from user input (server, username and password). IOW it worked for me because I typed values that produced a valid URL.

I do not know what you used as the user input for testing - but your application needs to validate that it leads to a valid URL (as user can type anything) otherwise your customers will experience crashes if they make some kind of mistakes while typing.


side note: The correct "-" replacement is "%2D" not "%96" (it looks the same but it's not the "minus" character) but that's would not lead to an invalid URL (but it likely won't match your server URL)
Comment 11 Curtis Traverse 2013-11-08 13:30:37 UTC
Just to follow up on this. I encoded the URL and now it seems to be working well. It might be worthwhile for the Xamarin framework to handle this exception/error as the Request object does not give any indication it is not valid when you assign it to the UIWebView.

The only notification you get is a null URL object in the event where we get the "object ref error"
Comment 12 Sebastien Pouliot 2013-11-08 14:23:50 UTC
Yes, this is a condition we already check but it does not cover 100% of the cases. There's already a bug for this: #14399