已发布程序上出现NullReferenceException,但Visual Studio中没有

本文关键字:Visual Studio NullReferenceException 程序上 | 更新日期: 2023-09-27 18:19:56

我在从发布位置打开或直接从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)

我确保在初始化程序之前填充了字符串,所以不应该加载任何为null的字符串,更不用说我在做任何事情之前都要确保CheckedList_Load中的字符串不为null。有什么想法吗?

编辑:我不知道为什么这被标记为重复。我的所有变量都不是空的,链接的答案并不能完全解决这个问题。创建的异常似乎认为已经启动的windows窗体在某种程度上为null,这将超出所链接问题的范围。

已发布程序上出现NullReferenceException,但Visual Studio中没有

创建受试者列表,验证为空

 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文件夹。否则,请检查debug中的bin文件夹。