[英]How to include non-compiled content alongside a C# program published via 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.