Xamarin iOS 应用程序中的空引用异常

Null reference exception in Xamarin iOS app

我正在尝试在我的应用程序中设置全局变量,例如颜色。即导航栏,我必须在每个视图控制器中单独设置的颜色。 我创建了一个助手 class、UIHelper.cs 来设置这些值,然后在别处调用它们。 在我的 MainViewController.cs 中,我正在创建 UIHelper 的实例并在这一行中调用它: this.NavigationController.NavigationBar.BarTintColor = uiHelper.SetNavBarRGB();

但是这是抛出一个System.NullReferenceException

System.NullReferenceException: Object reference not set to an instance of an object
  at NoteTaker.iOS.MainViewController.ViewDidLoad () [0x00018] in /Users/richardcurteis/Development/XamarinProjects/NoteTaker/iOS/MainViewController_.cs:28
  at at (wrapper managed-to-native) UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
  at UIKit.UIApplication.Main (System.String[] args, IntPtr principal, IntPtr delegate) [0x00005] in /Users/builder/data/lanes/2377/73229919/source/maccore/src/UIKit/UIApplication.cs:77
  at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x00038] in /Users/builder/data/lanes/2377/73229919/source/maccore/src/UIKit/UIApplication.cs:61
  at NoteTaker.iOS.Application.Main (System.String[] args) [0x00013] in /Users/richardcurteis/Development/XamarinProjects/NoteTaker/iOS/Main.cs:17

MainViewController.cs

using System;
using System.CodeDom.Compiler;
using UIKit;
using System.Collections.Generic;

namespace NoteTaker.iOS
{
    partial class MainViewController : UIViewController
    {
        List<Note> notes;
        UITableView table;
        UIHelper uiHelper;


        public MainViewController (IntPtr handle) : base (handle)
        {
            notes = new List<Note> ();
            UIHelper uiHelper = new UIHelper();
        }

        public override void ViewDidLoad ()

        {
            base.ViewDidLoad ();

            //this.NavigationController.NavigationBar.BarTintColor = UIColor.FromRGB (255,0,255);
            this.NavigationController.NavigationBar.BarTintColor = uiHelper.SetNavBarRGB(); // Exception being thrown here
            this.Title = "Notes";
            this.NavigationController.NavigationBar.TitleTextAttributes = new UIStringAttributes () { ForegroundColor = UIColor.White };

            table = new UITableView () {
                Frame = new CoreGraphics.CGRect (0, this.NavigationController.NavigationBar.Frame.Bottom, this.View.Bounds.Width, this.View.Bounds.Height - this.NavigationController.NavigationBar.Frame.Height),
            };
            this.View.Add (table);

            var addButton = new UIBarButtonItem (UIBarButtonSystemItem.Add);
            addButton.TintColor = UIColor.White;

            this.NavigationItem.RightBarButtonItems = new UIBarButtonItem[] { addButton };
            addButton.Clicked += (sender, e) => {
                this.NavigationController.PushViewController (new NoteViewController(), true);
            };

            notes = Database.getNotes ();
            table.Source = new NotesTableViewSource (notes, this);
        }

        public override void ViewWillAppear (bool animated)
        {
            base.ViewWillAppear (animated);
            notes = Database.getNotes ();
            table.Source = new NotesTableViewSource(notes, this );
        }
    }
}

UIHelper.cs

using System;
using UIKit;

namespace NoteTaker.iOS
{
    public class UIHelper : UIInputViewController
    {
        public UIHelper ()
        {
        }

        public UIColor SetNavBarRGB ()
        {
            var uiColour = UIColor.FromRGB (255, 0, 255);
            return uiColour;
        }
    }
}

你的 MainViewController 构造函数 (MainViewController (IntPtr handle)) 甚至被调用了吗?

您能否尝试检查 null 中的 uiHelper 变量,如果它为空,则从 UIHelper class.

初始化一个新实例
 public override void ViewDidLoad ()

        {
            base.ViewDidLoad ();
             If(uiHelper == null)
             {
                 uiHelper = new UIHelper(); 
             }
            //this.NavigationController.NavigationBar.BarTintColor = UIColor.FromRGB (255,0,255);
            this.NavigationController.NavigationBar.BarTintColor = uiHelper.SetNavBarRGB(); // Exception being thrown here
            this.Title = "Notes";
            this.NavigationController.NavigationBar.TitleTextAttributes = new UIStringAttributes () { ForegroundColor = UIColor.White };

            table = new UITableView () {
                Frame = new CoreGraphics.CGRect (0, this.NavigationController.NavigationBar.Frame.Bottom, this.View.Bounds.Width, this.View.Bounds.Height - this.NavigationController.NavigationBar.Frame.Height),
            };
            this.View.Add (table);

            var addButton = new UIBarButtonItem (UIBarButtonSystemItem.Add);
            addButton.TintColor = UIColor.White;

            this.NavigationItem.RightBarButtonItems = new UIBarButtonItem[] { addButton };
            addButton.Clicked += (sender, e) => {
                this.NavigationController.PushViewController (new NoteViewController(), true);
            };

            notes = Database.getNotes ();
            table.Source = new NotesTableViewSource (notes, this);
        }

您有冲突:

您将 uiHelper 声明为 class 定义中的字段:

partial class MainViewController : UIViewController
{
    List<Note> notes;
    UITableView table;
    UIHelper uiHelper;

然后在构造函数中声明另一个同名变量:

public MainViewController (IntPtr handle) : base (handle)
{
        notes = new List<Note> ();
        UIHelper uiHelper = new UIHelper();
}

这导致仅在构造函数的范围内声明和分配局部 uiHelper,但从未触及 class' 字段 uiHelper。因此它在 ViewLoad() 方法中为空。只需在构造函数中删除类型就可以了:

public MainViewController (IntPtr handle) : base (handle)
{
        notes = new List<Note> ();
        uiHelper = new UIHelper();
}