System.NullReferenceException on UIWebView

SatishBirajdarSatishBirajdar USMember ✭✭
public class MainClass : NSObject
    {
                        AppDelegate.GetInstance ().InvokeOnMainThread (delegate {
                                AttachmentWebViewController AVC = new AttachmentWebViewController ();
                                AppDelegate.GetInstance().NavController.PushViewController(AVC, true);
                                AVC.showAttachment (attachment);
                        });
}

        public partial class AttachmentWebViewController : UIViewController
        {   
                public override void ViewDidLoad ()
                {                       
                        base.ViewDidLoad ();
                }

                public void showAttachment(Attachment baseAttachment)
                {
                        string fileName = baseAttachment.FileName; 
                        string localDocUrl = Path.Combine (NSBundle.MainBundle.BundlePath, fileName);

                        if (new NSUrlRequest (new NSUrl (localDocUrl)) != null) {                       
                                Console.WriteLine ("new NSUrlRequest(new NSUrl(localDocUrl)) is NULL");
                        }

                        webView.LoadRequest(new NSUrlRequest(new NSUrl(localDocUrl)));
                        webView.ScalesPageToFit = true;

                }
        }

I am trying to call showAttachment() from MainClass, but I am getting error as "System.NullReferenceException-object reference not set to an instance of an object" on line "webView.LoadRequest(new NSUrlRequest(new NSUrl(localDocUrl)));" because my webView is Null.

I have added UIWebView in my .XIB manually.

Thanks

Posts

  • rschmidtrschmidt USMember ✭✭

    Does ViewDidLoad get called before you call showAttachment? If not, that's your problem. webView will remain null until the view loads.

  • SatishBirajdarSatishBirajdar USMember ✭✭

    @rschmidt "ViewDidLoad" isn't getting called before showAttachment(). But my question, is that the correct way to call a ViewController from NSObject class ?

    Or is there any other way to call ?

    Thanks

  • rschmidtrschmidt USMember ✭✭

    @SatishBirajdar this isn't really a problem with your MainClass class, it's a problem with AttachmentWebViewController which should be able to tolerate the case where clients call showAttachment before its view loads. Here is how you might handle that:

       public partial class AttachmentWebViewController : UIViewController
       {
          private NSUrlRequest pendingRequest;
    
          public override void ViewDidLoad ()
          {                       
             base.ViewDidLoad ();
             this.webView.ScalesPageToFit = true;
             this.LoadPendingRequest();
          }
    
          public void showAttachment(Attachment baseAttachment)
          {
             string fileName = baseAttachment.FileName; 
             string localDocUrl = Path.Combine (NSBundle.MainBundle.BundlePath, fileName);
    
             if (new NSUrlRequest (new NSUrl (localDocUrl)) != null) {                       
                Console.WriteLine ("new NSUrlRequest(new NSUrl(localDocUrl)) is NULL");
             }
    
             this.pendingRequest = new NSUrlRequest(new NSUrl(localDocUrl));
    
             if (this.IsViewLoaded)
             {
                this.LoadPendingRequest();
             }
          }
    
          private void LoadPendingRequest()
          {
             if (this.pendingRequest != null)
             {
                this.webView.LoadRequest(this.pendingRequest);
             }
          }
       }
    

    That way, if the client calls showAttachment before the view loads, then the class remembers the request and defers loading it until the view does load.

  • SatishBirajdarSatishBirajdar USMember ✭✭

    @rschmidt Thanks will try this.

Sign In or Register to comment.