这段 Winform C# 代码有什么问题?它会 运行 在 Windows 中很好,但在单声道(Linux)中,将抛出 NullReferenceException

What's wrong with this Winform C# code? It will run fine in Windows, but in mono(Linux), will throw a NullReferenceException

错误是:
System.NullReferenceException: Object reference not set to an instance of an object.
我知道这个错误发生在一个对象被使用但没有初始化或正在初始化但后来被声明为null但仍然被访问导致异常的情况下。但我无法弄清楚有问题的对象在哪里,所以我可以自己纠正它。该错误将在我 select 第二次从列表框控件输入字符串后显示。一开始它会工作,然后如果我第二次 select 另一个项目,程序会突然退出,错误将显示在 Linux.

的命令行中

如果需要,我可以在 运行 mono win_binary_file 之后显示完整的错误。

调试后,我发现这段有问题的代码会抛出 NullReferenceException:

public void fListItems(DataGridView datagridview1, string param_name)
    {
        NpgsqlDataAdapter dr = default(NpgsqlDataAdapter);
        DataSet ds;
        string sql;
        NpgsqlConnection dbcon;
        string connectionstr = "SERVER=" + DataBaseHost + ";DATABASE=" + DatabaseName + ";USER ID=" + DatabaseUser + ";PASSWORD=" + DatabasePassword + ";pooling=true; port=" + DatabasePort;
        dbcon = new NpgsqlConnection(connectionstr);
        try
        {
            sql = "SELECT * FROM purchase_order" +                     
           " WHERE purchase_request_num= '" + param_name + "' ORDER BY id ASC; ";                
            dbcon.Open();
            ds = new DataSet("purchase_order");
            dr = new NpgsqlDataAdapter();
            dr.SelectCommand = new NpgsqlCommand(sql, dbcon);
            dr.Fill(ds, "purchase_order");
            datagridview1.DataSource = ds.Tables["purchase_order"];
        } 
        catch (ApplicationException ex)
        {
            MessageBox.Show(ex.Message);
        }
        finally
        {
            dbcon.Close();
        }
    }

此代码将调用上面的函数:

private void listboxPR_SelectedIndexChanged(object sender, EventArgs e)
    {
        Cursor = Cursors.WaitCursor;
        string prnumberSelect;
        if (varname == 180)
        {
            //Somecode here for condition varname=180
            prnumberSelect = listboxPR.Text.ToString();
            Class1.fListItems(DataGridRequests, prnumberSelect);
        }
        else
        {
            prnumberSelect = listboxPR.Text.ToString();
            Class1.fListItems(DataGridRequests, prnumberSelect);
        }
        Cursor = Cursors.Default;

    }

完整错误:

System.NullReferenceException: Object reference not set to an instance of an object
  at purchase_requisition.frmmain.DataGridRequests_SelectionChanged (System.Object sender, System.EventArgs e) [0x00000] in <filename unknown>:0 
  at System.Windows.Forms.DataGridView.OnSelectionChanged (System.EventArgs e) [0x00000] in <filename unknown>:0 
  at System.Windows.Forms.DataGridView.SetSelectedRowCore (Int32 rowIndex, Boolean selected) [0x00000] in <filename unknown>:0 
  at System.Windows.Forms.DataGridView.SetSelectedRowCoreInternal (Int32 rowIndex, Boolean selected) [0x00000] in <filename unknown>:0 
  at (wrapper remoting-invoke-with-check) System.Windows.Forms.DataGridView:SetSelectedRowCoreInternal (int,bool)
  at System.Windows.Forms.DataGridViewBand.set_Selected (Boolean value) [0x00000] in <filename unknown>:0 
  at System.Windows.Forms.DataGridViewRow.set_Selected (Boolean value) [0x00000] in <filename unknown>:0 
  at System.Windows.Forms.DataGridView.ClearSelection () [0x00000] in <filename unknown>:0 
  at System.Windows.Forms.DataGridView.MoveCurrentCell (Int32 x, Int32 y, Boolean select, Boolean isControl, Boolean isShift, Boolean scroll) [0x00000] in <filename unknown>:0 
  at System.Windows.Forms.DataGridView.ClearBinding () [0x00000] in <filename unknown>:0 
  at System.Windows.Forms.DataGridView.set_DataSource (System.Object value) [0x00000] in <filename unknown>:0 
  at (wrapper remoting-invoke-with-check) System.Windows.Forms.DataGridView:set_DataSource (object)
  at purchase_requisition.dbquery.PRMonitoringGrid (System.Windows.Forms.DataGridView DatagridRequests, System.String Purchase_Number) [0x00000] in <filename unknown>:0 
  at purchase_requisition.frmmain.listboxPR_SelectedIndexChanged (System.Object sender, System.EventArgs e) [0x00000] in <filename unknown>:0 
  at System.Windows.Forms.ListBox.OnSelectedIndexChanged (System.EventArgs e) [0x00000] in <filename unknown>:0 
  at System.Windows.Forms.ListBox.OnItemClick (Int32 index) [0x00000] in <filename unknown>:0 
  at System.Windows.Forms.ListBox.OnMouseUpLB (System.Object sender, System.Windows.Forms.MouseEventArgs e) [0x00000] in <filename unknown>:0 
  at System.Windows.Forms.Control.OnMouseUp (System.Windows.Forms.MouseEventArgs e) [0x00000] in <filename unknown>:0 
  at System.Windows.Forms.Control.WmLButtonUp (System.Windows.Forms.Message& m) [0x00000] in <filename unknown>:0 
  at System.Windows.Forms.Control.WndProc (System.Windows.Forms.Message& m) [0x00000] in <filename unknown>:0 
  at System.Windows.Forms.ListBox.WndProc (System.Windows.Forms.Message& m) [0x00000] in <filename unknown>:0 
  at System.Windows.Forms.Control+ControlWindowTarget.OnMessage (System.Windows.Forms.Message& m) [0x00000] in <filename unknown>:0 
  at System.Windows.Forms.Control+ControlNativeWindow.WndProc (System.Windows.Forms.Message& m) [0x00000] in <filename unknown>:0 
  at System.Windows.Forms.NativeWindow.WndProc (IntPtr hWnd, Msg msg, IntPtr wParam, IntPtr lParam) [0x00000] in <filename unknown>:0 

我发现函数 fListItems() 根本没有问题。触发异常的是 DatagridREquests_SelectionChanged 事件的代码。我在 listboxPR_SelectedIndexChanged 事件下转移了有问题的代码,因为我需要的是在数据网格被函数 fListItems().

填充后从中获取数据
private void listboxPR_SelectedIndexChanged(object sender, EventArgs e)
{
    Cursor = Cursors.WaitCursor;
    string prnumberSelect;
    if (varname == 180)
    {
        //Somecode here for condition varname=180
        prnumberSelect = listboxPR.Text.ToString();
        Class1.fListItems(DataGridRequests, prnumberSelect);
        //Transferred code from DatagridRequests_SelectionChanged event
        if (DataGridRequests.RowCount > 0 ) {
            intVar1 = Convert.ToInt32(DataGridRequests.CurrentRow.Cells["column"].Value);
        }
    }
    else
    {
        prnumberSelect = listboxPR.Text.ToString();
        Class1.fListItems(DataGridRequests, prnumberSelect);
        if (DataGridRequests.RowCount > 0 ) {
            intVar1 = Convert.ToInt32(DataGridRequests.CurrentRow.Cells["column"].Value);
        }
    }
    Cursor = Cursors.Default;

}

谢谢大家帮助我....