繁体   English   中英

已发布程序上的 NullReferenceException 但在 Visual Studio 中没有

[英]NullReferenceException on published program but not in Visual Studio

我在从发布位置打开或直接从 bin 文件夹运行的该程序的任何实例上遇到问题,但在 Visual Studio 中没有,所以我很难理解发生了什么。 如果您单击继续,该程序仍将运行,据我所知,该程序不会丢失任何功能,但我并不希望人们必须点击该异常消息。

这是异常带来的代码:

public partial class CheckedList : Form
{
    public string[] list;
    public bool cancel;
    public List<string> chosen = new List<string>();

    public CheckedList(string[] _list)
    {
        list = _list;
        InitializeComponent();
    }

    private void CheckedList_Load(object sender, EventArgs e)
    {
        if (list != null)
        {
            foreach (string sub in list)
            {
                if (sub.Contains("Exception 1") == false && sub.Contains("Exception 2") == false && sub.Contains("Exception 3") == false)
                {
                    checkedListBox1.Items.Add(sub, true);
                }

            }
        }

    }

    private void button2_Click(object sender, EventArgs e)
    {
        cancel = true;
        this.Close();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        foreach(object item in checkedListBox1.CheckedItems)
        {
            chosen.Add(item.ToString());
        }

        this.Close();
    }
}

然后这里是调出表单的代码:

    string[] itemSubjects = new string[i];
    i = 0;
    foreach (Outlook.AppointmentItem appt in rangeAppts)
    {
        itemSubjects[i] = appt.Subject;
        i = i + 1;
    }
    CheckedList dialog = new CheckedList(itemSubjects);
    dialog.ShowDialog();

例外:

************** Exception Text **************
System.NullReferenceException: Object reference not set to an instance of an object.
   at Tool.CheckedList.CheckedList_Load(Object sender, EventArgs e)
   at System.Windows.Forms.Form.OnLoad(EventArgs e)
   at System.Windows.Forms.Form.OnCreateControl()
   at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
   at System.Windows.Forms.Control.CreateControl()
   at System.Windows.Forms.Control.WmShowWindow(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
   at System.Windows.Forms.ContainerControl.WndProc(Message& m)
   at System.Windows.Forms.Form.WmShowWindow(Message& m)
   at System.Windows.Forms.Form.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

我确保在初始化程序之前确保字符串已被填充,因此不应该加载任何为空的内容,更不用说我在对它进行任何操作之前确保 CheckedList_Load 中的字符串不为空。 有任何想法吗?

编辑:我不确定为什么这被标记为重复。 我的变量都不是空的,链接的答案没有完全解决这个问题。 创建的异常似乎认为显然已启动的 Windows 窗体在某种程度上为空,这超出了所链接问题的范围。

创建主题列表,验证为空

 List<string> itemSubjects = new List<string>();
    foreach (Outlook.AppointmentItem appt in rangeAppts)
    {
        if(!string.IsNullOrEmpty(appt.Subject))
        {
           itemSubjects.Add( appt.Subject);
         }
    }
    CheckedList dialog = new CheckedList(itemSubjects);
    dialog.ShowDialog();

更改构造函数以接受列表

   public List<string> list;
   public CheckedList(List<string> _list)
    {
        list = _list;
        InitializeComponent();
    }

如果您在发布模式下构建应用程序,请检查发布下的 bin 文件夹。 否则检查调试中的 bin 文件夹。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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