从 DialogResult 选择选项时出错

发布于 2024-12-14 04:52:23 字数 1331 浏览 0 评论 0原文

我在使用 YesNo 按钮在 MessageBox 上选择 Yes 时遇到问题。

Object reference not set to an instance of an object.

来自行:

AddEntryWindow addWindow = new AddEntryWindow
    (this, storedAuth.UserName, storedAuth.Password);

我不明白问题是什么,因为此后的几行是相同的语句。我该如何解决这个问题?

已修复

private void tsmiAddEntry_Click(object sender, EventArgs e)
{
    if (storedAuth == null)
    {
        DialogResult result = MessageBox.Show
            ("You must log in before you add an entry." 
            + Environment.NewLine + "You want to authenticate?",
            "Information", MessageBoxButtons.YesNo, 
            MessageBoxIcon.Information);

        if (result == DialogResult.Yes)
        {
            AuthenticationWindow authWindow = 
                new AuthenticationWindow();
            authWindow.ShowDialog();
            storedAuth = authWindow.Result;

            AddEntryWindow addWindow = new AddEntryWindow
                (this, storedAuth.UserName, storedAuth.Password);
            addWindow.ShowDialog();
        }
    }
    else
    {
        AddEntryWindow addWindow = new AddEntryWindow
            (this, storedAuth.UserName, storedAuth.Password);
        addWindow.ShowDialog();
    }
}

I am experiencing a problem while choosing Yes on my MessageBox with buttons Yes or No.

Object reference not set to an instance of an object.

From line:

AddEntryWindow addWindow = new AddEntryWindow
    (this, storedAuth.UserName, storedAuth.Password);

I don't understand what's the problem since few lines after this, is the same statement. How can I fix this?

Fixed

private void tsmiAddEntry_Click(object sender, EventArgs e)
{
    if (storedAuth == null)
    {
        DialogResult result = MessageBox.Show
            ("You must log in before you add an entry." 
            + Environment.NewLine + "You want to authenticate?",
            "Information", MessageBoxButtons.YesNo, 
            MessageBoxIcon.Information);

        if (result == DialogResult.Yes)
        {
            AuthenticationWindow authWindow = 
                new AuthenticationWindow();
            authWindow.ShowDialog();
            storedAuth = authWindow.Result;

            AddEntryWindow addWindow = new AddEntryWindow
                (this, storedAuth.UserName, storedAuth.Password);
            addWindow.ShowDialog();
        }
    }
    else
    {
        AddEntryWindow addWindow = new AddEntryWindow
            (this, storedAuth.UserName, storedAuth.Password);
        addWindow.ShowDialog();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

她说她爱他 2024-12-21 04:52:23

您正在访问 storedAuth 的属性,但在上面您检查了 storedAuth 为 null,因此此代码保证抛出 NullReferenceException...

You're accessing properties of storedAuth, but just above you checked that storedAuth is null, so this code is guaranteed to throw a NullReferenceException...

呢古 2024-12-21 04:52:23

看这个声明
if (storedAuth == null)

并且您访问 null 对象的属性。如果需要对象,则为该对象赋值,然后访问用户名和密码。

这是错误的原因..您不应在以下语句中使用storedAuth.UserName、storedAuth.Password)。使用一些默认值,例如“”或“默认”

AddEntryWindow addWindow = new AddEntryWindow
这个,storedAuth.UserName,storedAuth.Password);
addWindow.ShowDialog();

Look this statement
if (storedAuth == null)

and you accessing property of a null object. if it is necessary object then assign value to this object then access UserName and Password.

this is reason of error.. you should not use storedAuth.UserName, storedAuth.Password) in the following statement. use some default value like "" or "Default"

AddEntryWindow addWindow = new AddEntryWindow
(this, storedAuth.UserName, storedAuth.Password);
addWindow.ShowDialog();

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文