未处理 NullReferenceException错误

bulls5988 2011-11-09 10:44:22
读取数据部分被写到了类库m_class,在DataGridView窗体m_usermange.cs 中实例化了这个类,然后在m_useredit.cs修改,刷新m_usermange.cs 中的数据库的DGV控件内容。目前,修改读取都没问题,但是最后调用m_usermange.cs的刷新方法时,提示refresh.form_load()这句:"未处理 NullReferenceException错误" ,哪位高人能告诉我为什么。如果不用类库直接代码都写进窗体中就没问题。



m_class.dll

public class m_userlist
{
public DataGridView userlist_load(DataGridView u_data)
{

//读取用户列表

m_sql_conn fee_conn = new m_sql_conn();
SqlConnection conn = new SqlConnection(fee_conn.sql_conn());
SqlCommand cmd = new SqlCommand(userload,conn);
find_do = new SqlDataAdapter();
find_do.SelectCommand = cmd;
ds = new DataSet();
try
{
u_data.RowHeadersVisible = false;
find_do.Fill(ds, "rs");
}
catch(System.Exception)
{
MessageBox.Show("数据库连接失败", "数据读未读取", MessageBoxButtons.OK);
return null;
}
u_data.DataSource = ds.Tables["rs"];

for (int k = 0; k < u_data.Columns.Count; k++)
{
u_data.Columns[k].SortMode = DataGridViewColumnSortMode.NotSortable;
}
return u_data;
}
}





DataGridView控件所在窗体 m_usermange.cs DGV控件名:d_userlist

private void formload()
{
m_userlist users = new m_userlist();
users.userlist_load(d_userlist);
}

public void form_load()
{
formload();
}





修改数据库窗体 m_useredit.cs 代码:

[code=C#]
m_userlist card_up = new m_userlist();
card_up.user_save(int.Parse(m_usermange.user_id), t_pwd.Text, t_rname.Text, flag, sale, usermanage,
salemanage, allreport,classreport,dbset,dbmanage, deadcard, cardnumset, cardmanage);
m_usermange refresh = new m_usermange();
refresh = (m_usermange)this.Owner;
refresh.form_load();

...全文
201 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
holycjj 2011-11-09
  • 打赏
  • 举报
回复
你应该调用refresh.formload()而不是refresh.form_load()吧
holycjj 2011-11-09
  • 打赏
  • 举报
回复
你在其它地方调用窗体的load事件处理函数?那个事件没有触发,那当然是null值。引用肯定出错啊。
bulls5988 2011-11-09
  • 打赏
  • 举报
回复
这个填充DGV和保存修改数据 都没问题。都不会报错,只有这个刷新窗体才报错
bulls5988 2011-11-09
  • 打赏
  • 举报
回复
这个填充DGV和保存修改数据 都没问题。都不会报错,只有这个刷新窗体才保存
sp951 2011-11-09
  • 打赏
  • 举报
回复
public DataGridView userlist_load(DataGridView u_data)
{
....
}
这里面问题。
这种NullReference问题,他提示你的地方设个断点调试就知道!
holycjj 2011-11-09
  • 打赏
  • 举报
回复
没必要把整个窗体 对象都搞过来吧。当然那样也行
holycjj 2011-11-09
  • 打赏
  • 举报
回复
我说的是你在m_useredit.cs中应该调用refresh.formload()而不是refresh.form_load()。你在m_usermange.cs中把
public void form_load()
{
formload();
}
改成
public void form_load()
{
m_userlist users = new m_userlist();
users.userlist_load(d_userlist);
}
那不是跟没改一样么。关键是你在m_useredit.cs中不能调用m_usermange.cs中的load事件处理函数
bulls5988 2011-11-09
  • 打赏
  • 举报
回复
找到啦,调用时少了:edit.Owner = this;
bulls5988 2011-11-09
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 holycjj 的回复:]
你应该调用refresh.formload()而不是refresh.form_load()吧
[/Quote]

并不是这样的,我这样写,

public void form_load()
{
m_userlist users = new m_userlist();
users.userlist_load(d_userlist);
}

也照样是提示: 未处理 System.NullReferenceException
Message="未将对象引用设置到对象的实例。"
Source="中影会员卡"
StackTrace:
在 中影会员卡.m_useredit.b_save_Click(Object sender, EventArgs e) 位置 C:\middlemovie\中影会员卡\m_useredit.cs:行号 172
在 System.Windows.Forms.Control.OnClick(EventArgs e)
在 System.Windows.Forms.Button.OnClick(EventArgs e)
在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
在 System.Windows.Forms.Control.WndProc(Message& m)
在 System.Windows.Forms.ButtonBase.WndProc(Message& m)
在 System.Windows.Forms.Button.WndProc(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
在 System.Windows.Forms.Form.ShowDialog(IWin32Window owner)
在 System.Windows.Forms.Form.ShowDialog()
在 中影会员卡.m_usermange.b_edit_Click(Object sender, EventArgs e) 位置 C:\middlemovie\中影会员卡\m_usermange.cs:行号 51
在 System.Windows.Forms.Control.OnClick(EventArgs e)
在 System.Windows.Forms.Button.OnClick(EventArgs e)
在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
在 System.Windows.Forms.Control.WndProc(Message& m)
在 System.Windows.Forms.ButtonBase.WndProc(Message& m)
在 System.Windows.Forms.Button.WndProc(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
在 System.Windows.Forms.Application.Run(Form mainForm)
在 中影会员卡.Program.Main() 位置 C:\middlemovie\中影会员卡\Program.cs:行号 29
在 System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
在 System.Threading.ThreadHelper.ThreadStart()
InnerException:
================================================================ 名 称:SubSonic插件 版 本:3.0.0.4.1 最后更新:2013-8-10 官 网: http://www.subsonicproject.com/ BUG 修改:Empty(AllEmpty) Email: 1654937@qq.com 学习讨论:SubSonic3.0学习群(327360708) 相关博客:http://www.cnblogs.com/EmptyFS/ ================================================================ 本版修复了下面问题: 1、Structs.tt模版生成时,没有给PropertyName属性赋值引起的空指针异常(System.NullReferenceException异常) 2、使用SubSonic.Query.Select和SubSonic.Query.SqlQuery查询时,设置了Top属性后,产生的“未处理InvalidOperationException异常(关键字'TOP'附近有语法错误)”Bug 3、生成的表名自动加复数(s)产生的“用户代码未处理SqlException,对象名'xxxs'无效”异常 4、使用SubSonic.Query.Select和SubSonic.Query.SqlQuery查询时,字段类型为tinyint时列丢失问题的Bug修复 5、为SubSonic.Query.Select和存储过程查询添加更多的执行功能(返回内容) 至于使用Json反序列化获得的实体进行更新操作时,只能执行添加而不能执行修改(编辑)操作的问题,这种操作问题的修改,我想应该比较少人使用,所以就不加到代码中,如果有需要的用户请登陆我的博客对着修改就可以了。 更多关于SubSonic3.0的信息,请访问博客:http://www.cnblogs.com/EmptyFS/

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧