错误是:
System.NullReferenceException: Object reference not set to an instance of an object.
我知道在使用某个对象但未初始化或初始化但后来被声明为null但仍被访问导致异常时会发生此错误。但我无法弄清楚违规物品的位置,所以我可以自己纠正。在我第二次从列表框控件中选择一个字符串后,将显示错误。首先它会起作用然后如果我第二次选择另一个项目,程序将突然退出,错误将显示在Linux的命令行中。
如果需要,我可以在运行单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
答案 0 :(得分: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;
}
谢谢你们帮助我......