System.NullReferenceException 在 Xamarin 中
System.NullReferenceException in Xamarin
我写了下面这行代码来检查用户是否在 SettingViewController 中输入了密码。我正在使用以下代码进行测试,在用户导航到 settingViewController page.However 之前,我收到 SystemNullReference 错误。
SettingViewController callSetting = this.Storyboard.InstantiateViewController ("SettingViewController") as SettingViewController;
if (string.IsNullOrEmpty(callSetting.sSettings.password)==true)
Console.WriteLine("is null or empty");
else
Console.WriteLine (callSetting.sSettings.password);
这是我的服务器设置 Class:
public class ServerSettings
{
public string server{ get; set;}
public string port { get; set;}
public string username { get; set;}
public string password { get; set;}
public string userid { get; set;}
public ServerSettings ()
{
}
}
这是我的 SettingViewController class:
partial class SettingViewController : UIViewController
{
public ServerSettings sSettings;
public SettingViewController (IntPtr handle) : base (handle)
{
this.Title = "Settings";
}
public override void ViewWillDisappear (bool animated)
{
sSettings.server=serverTF.Text;
sSettings.port = portTF.Text;
sSettings.password = passwordTF.Text;
sSettings.userid = inboxuserTF.Text;
sSettings.username = usernameTF.Text;
}
}
根据您提供的代码,有两种不同的可能性。
A) callSetting
是 null
B) callSetting.sSettings
是 null
.
您可以添加如下内容:
SettingViewController callSetting = this.Storyboard.InstantiateViewController ("SettingViewController") as SettingViewController;
if (callSetting == null)
throw new Exception("callSetting is null"); // Or if you can handle having a null callSetting then correct for it, but realistically this is a problem, so I'd throw an Exception
if (callSetting.sSetting == null)
throw new Exception("sSetting is null"); // Or if you can handle having a null callSetting.sSetting then correct it (such as using a default value).
if (string.IsNullOrEmpty(callSetting.sSettings.password)==true)
Console.WriteLine("is null or empty");
else
Console.WriteLine (callSetting.sSettings.password);
另请注意,您可以简化此行
if (string.IsNullOrEmpty(callSetting.sSettings.password)==true)
到
if (string.IsNullOrEmpty(callSetting.sSettings.password))
编辑:根据编辑后的问题,您需要将代码修改为
public SettingViewController (IntPtr handle) : base (handle)
{
this.Title = "Settings";
this.sSettings = new ServerSettings();
}
我写了下面这行代码来检查用户是否在 SettingViewController 中输入了密码。我正在使用以下代码进行测试,在用户导航到 settingViewController page.However 之前,我收到 SystemNullReference 错误。
SettingViewController callSetting = this.Storyboard.InstantiateViewController ("SettingViewController") as SettingViewController;
if (string.IsNullOrEmpty(callSetting.sSettings.password)==true)
Console.WriteLine("is null or empty");
else
Console.WriteLine (callSetting.sSettings.password);
这是我的服务器设置 Class:
public class ServerSettings
{
public string server{ get; set;}
public string port { get; set;}
public string username { get; set;}
public string password { get; set;}
public string userid { get; set;}
public ServerSettings ()
{
}
}
这是我的 SettingViewController class:
partial class SettingViewController : UIViewController
{
public ServerSettings sSettings;
public SettingViewController (IntPtr handle) : base (handle)
{
this.Title = "Settings";
}
public override void ViewWillDisappear (bool animated)
{
sSettings.server=serverTF.Text;
sSettings.port = portTF.Text;
sSettings.password = passwordTF.Text;
sSettings.userid = inboxuserTF.Text;
sSettings.username = usernameTF.Text;
}
}
根据您提供的代码,有两种不同的可能性。
A) callSetting
是 null
B) callSetting.sSettings
是 null
.
您可以添加如下内容:
SettingViewController callSetting = this.Storyboard.InstantiateViewController ("SettingViewController") as SettingViewController;
if (callSetting == null)
throw new Exception("callSetting is null"); // Or if you can handle having a null callSetting then correct for it, but realistically this is a problem, so I'd throw an Exception
if (callSetting.sSetting == null)
throw new Exception("sSetting is null"); // Or if you can handle having a null callSetting.sSetting then correct it (such as using a default value).
if (string.IsNullOrEmpty(callSetting.sSettings.password)==true)
Console.WriteLine("is null or empty");
else
Console.WriteLine (callSetting.sSettings.password);
另请注意,您可以简化此行
if (string.IsNullOrEmpty(callSetting.sSettings.password)==true)
到
if (string.IsNullOrEmpty(callSetting.sSettings.password))
编辑:根据编辑后的问题,您需要将代码修改为
public SettingViewController (IntPtr handle) : base (handle)
{
this.Title = "Settings";
this.sSettings = new ServerSettings();
}