什么可能导致这个非崩溃的NRE?

时间:2015-01-14 00:46:32

标签: c# sqlite compact-framework windows-ce nullreferenceexception

从主窗体的Load事件中调用有问题的方法 PopulateTransactionListBoxWithWorkTables()

private void frmMain_Load(object sender, EventArgs e)
{
    ExceptionLoggingService.Instance.WriteLog("Reached frmMain.frmMain_Load");
    ServicePointManager.CertificatePolicy = new TrustAllCertificatesPolicy();
    PopulateTransactionListBoxWithWorkTables();
}

在该方法的某处,它以NRE失败;我已经在一堆日志信息中撒了一层以查看它失败的地方:

private void PopulateTransactionListBoxWithWorkTables()
{
    ExceptionLoggingService.Instance.WriteLog("Reached 
frmMain.PopulateTransactionListBoxWithWorkTable");
    menuItemSEND_Deliveries.Enabled = false;
    menuItemSEND_Inventories.Enabled = false;

    try
    {
        listBoxWork.Items.Clear();

        ExceptionLoggingService.Instance.WriteLog("Reached 
frmMain.PopulateTransactionListBoxWithWorkTable#2");
        if (!hhsdbutils.TableExists("WorkTable"))
        {
            ExceptionLoggingService.Instance.WriteLog("Reached 
frmMain.PopulateTransactionListBoxWithWorkTable#3a");
            String msg = HHSConsts.NO_CURRENT_WORK;
listBoxWork.Items.Add(msg);
        }
        else
        {
            ExceptionLoggingService.Instance.WriteLog("Reached 
frmMain.PopulateTransactionListBoxWithWorkTable#3b");
            List<String> workTables = hhsdbutils.GetWorkTableNames();
menuItemTopSend.Enabled = workTables.Count > 0;

            foreach (String wt in workTables)
            {
                // TODO: This will cause enabled to be set to true each time a corresponding 
item is found, which should be improved
                if (wt.IndexOf("DSD") == 0)
                {
                    menuItemSEND_Deliveries.Enabled = true;
                }
                if (wt.IndexOf("INV") == 0)
                {
                    menuItemSEND_Inventories.Enabled = true;
                }
                listBoxWork.Items.Add(wt);
            }
            ExceptionLoggingService.Instance.WriteLog("Reached 
frmMain.PopulateTransactionListBoxWithWorkTable#4");
            if (listBoxWork.Items.Count > 0)
            {
                listBoxWork.SelectedIndex = 0;
            }
        }
   }
   catch (Exception ex)
   {
       String msgInnerExAndStackTrace = String.Format(
     "{0}; Inner Ex: {1}; Stack Trace: {2}", ex.Message, ex.InnerException, ex.StackTrace);
        MessageBox.Show(String.Format("Exception in    
            PopulateTransactionListBoxWithWorkTable(): {0}", msgInnerExAndStackTrace));
       ExceptionLoggingService.Instance.WriteLog(String.Format("From 
frmMain.PopulateTransactionListBoxWithWorkTable: {0}", msgInnerExAndStackTrace));
    }
} // populateListBoxWithWorkTableData

日志文件显示它到达PopulateTransactionListBoxWithWorkTables()中的#2,但不到#3(a或b)那里:

Message: Reached frmMain.frmMain_Load

Date: 4/3/2009 2:13:28 AM
Message: Reached frmMain.PopulateTransactionListBoxWithWorkTable

Date: 4/3/2009 2:13:28 AM
Message: Reached frmMain.PopulateTransactionListBoxWithWorkTable#2

Date: 4/3/2009 2:13:31 AM
Message: Reached frmMain.frmMain_Activated

Date: 4/3/2009 2:13:31 AM
Message: From frmMain.PopulateTransactionListBoxWithWorkTable: NullReferenceException; Inner Ex: ; Stack Trace:    at HHS.frmMain.PopulateTransactionListBoxWithWorkTables()
   at HHS.frmMain.frmMain_Load(Object sender, EventArgs e)
   at System.Windows.Forms.Form.OnLoad(EventArgs e)
   at System.Windows.Forms.Form._SetVisibleNotify(Boolean fVis)
   at System.Windows.Forms.Control.set_Visible(Boolean value)
   at System.Windows.Forms.Application.Run(Form fm)
   at HHS.Program.Main()

...所以在调用TableExists()时它会死掉;这是方法:

bool IHHSDBUtils.TableExists(string tableName)
{
    ExceptionLoggingService.Instance.WriteLog(String.Format("Reached 
SQliteHHSDBUtils.TableExists ({0})", tableName));
    // If the database file itself does not exist, there's no way the table could exist, so cut 
to the chase:
    if (!File.Exists(expectedSQLiteLocAndName)) return false;
    . . .

... TableExistsis开始时的日志消息没有被记录,所以它一到达就会爆炸......为什么会出现这种情况?!

注意: frmMain_Activated()除了日志字符串写入外没有其他代码。

0 个答案:

没有答案