简体   繁体   中英

NullReferenceException on published program but not in Visual Studio

I'm having an issue on any instance of this program opened from the published location or run directly from the bin folder, but not in Visual Studio, so I'm struggling to understand what's going on. The program will still run if you click continue, and as far as I'm aware loses no functionality, but the exception message is not really something I'd like people to have to click through.

Here's the code brought up by the exception:

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

And then here's the code that's bringing up the form:

    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:

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

I made sure to ensure the string was filled before initializing the program, so nothing should be loaded that's null, not to mention I am ensuring that the string in CheckedList_Load is not null before doing anything with it. Any ideas?

EDIT: I'm not sure why this is marked as duplicate. None of my variables are null and the answer linked does not fully address this issue. The exception created seems to think a windows form that has obviously been initiated is somehow null, which would be outside the scope of the question linked.

create list of subjects, validate for null

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

change the constructor to accept the list

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

if you build application on release mode, check bin folder under release. otherwise check the bin folder inside debug.

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