提问人:B. Clay Shannon-B. Crow Raven 提问时间:1/1/2015 最后编辑:B. Clay Shannon-B. Crow Raven 更新时间:1/28/2015 访问量:554
为什么下放填充的组合框会使窗体崩溃?
Why would dropping down a populated combo box crash a form?
问:
我有一对组合框,填充方式如下:
private void PopulateDeptCombox()
{
try
{
ExceptionLoggingService.Instance.WriteLog("Reached FrmInventory.PopulateDeptCombox");
List<String> depts = hhsdbutils.GetDeptItems();
foreach (String dept in depts)
{
comboBoxsDept.Items.Add(dept);
comboBoxeDept.Items.Add(dept);
}
if (comboBoxsDept.Items.Count > 0)
{
comboBoxsDept.SelectedIndex = 0;
}
if (comboBoxeDept.Items.Count > 0)
{
comboBoxeDept.SelectedIndex = comboBoxeDept.Items.Count - 1;
}
}
catch (Exception ex)
{
String msgInnerExAndStackTrace = String.Format(
"{0}; Inner Ex: {1}; Stack Trace: {2}", ex.Message, ex.InnerException, ex.StackTrace);
ExceptionLoggingService.Instance.WriteLog(String.Format("From FrmInventory.PopulateDeptCombox(): {0}", msgInnerExAndStackTrace));
}
}
GetDeptItems() 现在只返回几个项目:
List<string> IHHSDBUtils.GetDeptItems()
{
ExceptionLoggingService.Instance.WriteLog("Reached SQliteHHSDBUtils.GetDeptItems");
List<String> depts = new List<String> { "dept 1", "dept 2", "dept 3", "Test dept" };
// TODO: Replace this test data
return depts;
}
当我第一次打开它时,这个表单崩溃了,直到我尝试将 PopulateDeptCombox() 封闭起来。catch 块。它不再立即崩溃,但仍会在日志文件中记录 NRE:
Date: 3/21/2009 1:41:27 AM
Message: Reached FrmInventory.PopulateDeptCombox
Date: 3/21/2009 1:41:27 AM
Message: From FrmInventory.PopulateDeptCombox(): NullReferenceException; Inner Ex: ; Stack Trace: at
HHS.FrmInventory.PopulateDeptCombox()
at HHS.FrmInventory..ctor(String inventoryName, String siteNum, Boolean allowNewItems)
at HHS.frmNewInventory.buttonOK_Click(Object sender, EventArgs e)
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.ButtonBase.WnProc(WM wm, Int32 wParam, Int32 lParam)
at System.Windows.Forms.Control._InternalWnProc(WM wm, Int32 wParam, Int32 lParam)
at Microsoft.AGL.Forms.EVL.EnterModalDialog(IntPtr hwnModal)
at System.Windows.Forms.Form.ShowDialog()
at HHS.frmMain.menuItemFILE_NewInventory_Click(Object sender, EventArgs e)
at System.Windows.Forms.MenuItem.OnClick(EventArgs e)
at System.Windows.Forms.Menu.ProcessMnuProc(Control ctlThis, WM wm, Int32 wParam, Int32 lParam)
at System.Windows.Forms.Form.WnProc(WM wm, Int32 wParam, Int32 lParam)
at System.Windows.Forms.Control._InternalWnProc(WM wm, Int32 wParam, Int32 lParam)
at Microsoft.AGL.Forms.EVL.EnterMainLoop(IntPtr hwnMain)
at System.Windows.Forms.Application.Run(Form fm)
at HHS.Program.Main()
...但忽略了 NRE 和士兵(表现得好像它没有注意到 NRE)。
然后,但是,如果我单击 comboBoxsDept 来检查它包含哪些项目,它首先会“沙漏”一个咒语,然后它会下降,显示我期望看到的项目——但只是眨眼——然后表单崩溃了。“事后分析”(日志文件)显示上面的 NRE,以及日志文件末尾的 NRE:
Date: 3/21/2009 1:42:08 AM
Message: Reached FrmInventory.BarcodeReader_ReadNotify
Date: 3/21/2009 1:42:08 AM
Message: From application-wide exception handler: System.NullReferenceException: NullReferenceException
at HHS.FrmInventory.BarcodeReader_ReadNotify(Object sender, EventArgs e)
at System.Windows.Forms.Control.TASK.Invoke()
at System.Windows.Forms.Control._InvokeAll()
at System.Windows.Forms.Control.WnProc(WM wm, Int32 wParam, Int32 lParam)
at System.Windows.Forms.Control._InternalWnProc(WM wm, Int32 wParam, Int32 lParam)
at Microsoft.AGL.Forms.EVL.EnterMainLoop(IntPtr hwnMain)
at System.Windows.Forms.Application.Run(Form fm)
at HHS.Program.Main()
因此,还有另一个NRE,与条形码扫描码有关;由于某种原因,这段代码是 NRE 疯狂的。我有其他具有相同条形码扫描设置代码的表格,但它们的行为不是这样的。一切都很好,直到我扫描一个值,然后下拉 comboBoxsDept。
填充的组合框如何引出 NRE?
更新
如果我首先将某些内容扫描到条形码可扫描的文本框中,则可以防止崩溃。如果我直接进入下拉列表,它就会崩溃。因此,条形码扫描代码显然有些奇怪/令人毛骨悚然。
顺便说一句,这是我今年的最后一个问题;今晚不要喝太多辛辣的V8。
更新 2
对 Grant Winney 的回答:这是我的重载构造函数(称为):
public FrmInventory(string inventoryName, string siteNum, bool allowNewItems)
{
ExceptionLoggingService.Instance.WriteLog("Reached FrmInventory's overloaded constructor");
InitializeComponent();
_siteNum = siteNum;
_allowNewItems = allowNewItems;
_invName = inventoryName;
invFileName = Path.GetFileNameWithoutExtension(HHSUtils.GetGeneratedINVFileName(_siteNum));
recordDate = HHSUtils.ConvertDateTimeToSQLiteFormat(DateTime.Now);
comboBoxUPC_PLU.SelectedIndex = 0;
HookupHandlers();
PopulateDeptCombox();
PopulateSubdeptCombox();
SetRequiredControls();
textBoxUPC_PLU.Focus();
}
更新 3
这最终成为“涵洞”:
private void textBoxUPC_LostFocus(object sender, EventArgs e)
{
ExceptionLoggingService.Instance.WriteLog("Reached frmVerify.textBoxUPC_LostFocus");
this.DisposeBarcodeReaderAndData();
}
private void DisposeBarcodeReaderAndData()
{
ExceptionLoggingService.Instance.WriteLog("Reached
FrmInventory.DisposeBarcodeReaderAndData");
// If we have a reader
if (this.barcodeReader != null)
{
// Disable the reader
this.barcodeReader.Actions.Disable();
// Free it up
this.barcodeReader.Dispose();
// Indicate we no longer have one
this.barcodeReader = null;
}
// If we have a reader data
if (this.barcodeReaderData != null)
{
// Free it up
this.barcodeReaderData.Dispose();
// Indicate we no longer have one
this.barcodeReaderData = null;
}
}
一旦我注释掉了对DisposeBarcodeReaderAndData()的调用--使该方法成为死代码--太阳出来了,鸟儿开始唱歌,它们挂上了发明工作的混蛋,然后掉下来不再导致崩溃。
更新 4
问题又回来了,爸爸——炸它!我不得不将我的一些构造函数代码移动到窗体的 Load() 事件中,以使其直立并正确飞行。
答: 暂无答案
评论