我有一对像这样填充的组合框:
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;
}
当我第一次打开它时,此表单崩溃,直到我在try..catch块中包含PopulateDeptCombox()。它不再立即崩溃 ,但它仍然在日志文件中记录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来检查它包含哪些项目,它首先是一个“沙漏”,然后它会下降,显示我希望看到的项目 - 但仅限于一个眨眼 - 然后形式崩溃。 “post mortem”(日志文件)显示上面的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;由于某种原因,这段代码是疯狂的。我有其他形式与相同的条形码扫描设置代码,但他们不这样做。一切都很好,直到我扫描一个值,然后下拉comboBoxsDept。
填充的组合框如何引出NRE?
如果我首先将某些内容扫描到条形码可扫描文本框中,则会阻止崩溃。如果我直接进入下拉列表,它会崩溃。因此条形码扫描代码显然有些奇怪/可怕。
顺便说一下,这是我今年的最后一个问题;今晚不要喝太多辛辣的V8。回答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();
}
这最终成了“涵洞”:
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()的调用 - 让这个方法失效 - 太阳出来了,鸟儿开始唱歌,他们挂起发明作品的混蛋,然后掉线下来不再导致崩溃。
问题回来了,爸爸爆炸了!我不得不将我的一些构造函数代码移动到表单的Load()事件中,以使其变直并向右飞行。