简体   繁体   中英

System.NullReferenceException in Xamarin

I have written the following line of code to check whether or not user enters password in the SettingViewController. I am testing with the following code, before user navigate to settingViewController page.However, I am getting SystemNullReference Error.

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);

Here is my ServerSettings 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 ()
    {

    }

}

Here is my 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;
    }
}

在此处输入图片说明

Based on the code you provided, there's two different possibilities.

A) callSetting is null

B) callSetting.sSettings is null .

You could add something like this:

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);

Also as a side note, you could simplify this line

 if (string.IsNullOrEmpty(callSetting.sSettings.password)==true) 

to

 if (string.IsNullOrEmpty(callSetting.sSettings.password)) 

Edit: Based on the edited question, you'll need to modify your code to this

public SettingViewController (IntPtr handle) : base (handle)
{
    this.Title = "Settings";
    this.sSettings = new ServerSettings();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM