从类中的方法返回值时发生C#System.NullReferenceException异常
我已经在名为从类中的方法返回值时发生C#System.NullReferenceException异常,c#,nullreferenceexception,C#,Nullreferenceexception,我已经在名为clsFoldingManagement的类中创建了方法名fetchProjectsForUpdateQty。当通过函数(以数据集的形式)返回值数据时,我在winform组合框上收到一个错误,即对象引用未设置为对象的实例。下面是我在类中编写的方法fetchProjectsForUpdateQty代码clsFoldingManagement public static DataSet fetchProjectsForUpdateQty(int client_id) {
clsFoldingManagement
的类中创建了方法名fetchProjectsForUpdateQty
。当通过函数(以数据集的形式)返回值数据时,我在winform组合框上收到一个错误,即对象引用未设置为对象的实例。下面是我在类中编写的方法fetchProjectsForUpdateQty
代码clsFoldingManagement
public static DataSet fetchProjectsForUpdateQty(int client_id)
{
using (var con = new SqlConnection(ConStr))
{
string query = "select tblClient.ProjectName, tblFolding.Name , tblFolding.FoldingID from tblStockManagement LEFT OUTER JOIN tblClient ON tblClient.Client_ID=tblStockManagement.Client_ID LEFT OUTER JOIN tblFolding ON tblFolding.FoldingID=tblStockManagement.Client_ID where tblStockManagement.client_id=@clientid and tblStockManagement.quantity >0";
using (var cmd = new SqlCommand(query, con))
{
con.Open();
cmd.Parameters.Add("@clientid", SqlDbType.Int).Value = client_id;
cmd.ExecuteNonQuery();
SqlDataAdapter da = new SqlDataAdapter(cmd.CommandText, con);
DataSet ds = new DataSet();
da.Fill(ds, "DATA");
return ds;
}
}
}
我在我的组合框点击事件(cbUpdateProject\u点击
)中编写了以下代码
以下是我获得的异常详细信息:
System.NullReferenceException was unhandled
HResult=-2147467261
Message=Object reference not set to an instance of an object.
Source=FazalConstructions
StackTrace:
at FazalConstructions.frmStockMoving.cbUpdateProject_Click(Object sender, EventArgs e) in e:\Waqas Folder\FazalConstruction\FazalConstructions\FazalConstructions\frmStockMoving.cs:line 87
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.ComboBox.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at FazalConstructions.Program.Main() in e:\Waqas Folder\FazalConstruction\FazalConstructions\FazalConstructions\Program.cs:line 19
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
对象引用未设置为对象的实例
是我最喜欢抛出的错误之一,它的意思与它所说的完全一致。代码中引用的某个对象未设置为对象的实例
首先,最好确定引发异常的代码行
然后分析行中的对象,确保它们不为null。如果您说的是cbUpdateProject.DataSource=ds.Tables[“DATA”].DefaultView
和ds.Tables[“DATA”]
为空,但您尝试使用DefaultView
时将获得对象引用(ds.Tables[“DATA”]
)未设置为对象的实例,因为它为空
如果方法中的所有对象都是必需的,那么应该抛出一些异常或正确处理它们
if (clsFoldingManagement == null) throw new Exception ("The class is not initialized");
if (ds.Tables["DATA"] == null) throw new Exception ("The DATA table in the dataset is not initialized");
if (cbUpdateProject.SelectedValue == null) throw new Exception ("The combobox selected value is not initialized");
cbUpdateItemName.DataSource = null;
//DataSet data = clsFoldingManagement.fetchProjectDetails();
DataSet ds = clsFoldingManagement.fetchProjectsForUpdateQty(int.Parse(cbUpdateProject.SelectedValue.ToString()));
cbUpdateProject.DataSource = ds.Tables["DATA"].DefaultView;
cbUpdateProject.DisplayMember = "ProjectName";
cbUpdateProject.ValueMember= "Client_ID";
哪一行正在抛出错误,堆栈跟踪是什么?您应该发布异常,并提供完整的堆栈跟踪。我将函数返回值赋给数据集名称“ds”数据集ds=clsFoldingManagement.fetchProjectsForUpdateQty(int.Parse(cbUpdateProject.SelectedValue.ToString());用异常详细信息更新了问题最有可能的罪魁祸首似乎是cbUpdateProject。在您标识的行上选择了值
,是否检查以确保此对象不为空?
if (clsFoldingManagement == null) throw new Exception ("The class is not initialized");
if (ds.Tables["DATA"] == null) throw new Exception ("The DATA table in the dataset is not initialized");
if (cbUpdateProject.SelectedValue == null) throw new Exception ("The combobox selected value is not initialized");
cbUpdateItemName.DataSource = null;
//DataSet data = clsFoldingManagement.fetchProjectDetails();
DataSet ds = clsFoldingManagement.fetchProjectsForUpdateQty(int.Parse(cbUpdateProject.SelectedValue.ToString()));
cbUpdateProject.DataSource = ds.Tables["DATA"].DefaultView;
cbUpdateProject.DisplayMember = "ProjectName";
cbUpdateProject.ValueMember= "Client_ID";