在c#.net中创建新的objet引用null异常 [英] create new objet reference null exception in c#.net

查看:123
本文介绍了在c#.net中创建新的objet引用null异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


提出节点mousemove时,我在Windows窗体应用程序中有一个树形视图,我需要将node标签文本分配给某些authore属性,但这是一个例外,任何人都可以帮助我......
这是我的代码


i have a treeview in windows form application when the node mousemove is raised i need to assign the the node tag text in to some authore property but it is throunig an exception any one pls help me ......
here is my code

private void treeview1_MouseMove(object sender, MouseEventArgs e)
   {
   TreeNode theNode = browserControl1.trvObjects.GetNodeAt(e.X, e.Y);
   string testtext=theNode.Tag.ToString();  //Here it is thrwing an exception as Object reference not set to an instance of an object.
                                            //use new key word to create new object
   }




[edit]已添加代码块,将我的内容作为纯文本..."选项已禁用-OriginalGriff [/edit]




[edit]Code block added, "Treat my content as plain text..." option disabled - OriginalGriff[/edit]

推荐答案

有两种可能性:
1)theNode null
2)theNode.Tagnull

在代码中放置一个断点,然后找出其中的一个.
如果是前者,则需要查看MouseMove处理程序,并可能测试null并将其排除,以免出现鼠标在TreeView中但不在节点上的情况.
如果是后者,则需要查看Tag设置代码.
There are two possibilities:
1) theNode is null
2) theNode.Tag is null

Put a breakpoint in your code and find out which.
If it is the former, then you need to look at your MouseMove handler, and possibly test for null and exclude it to avoid times whenteh mouse if in the TreeView, but not over a node .
If the latter, then you need to look at your Tag setting code.


为什么不调试代码以找出哪个值为null,然后确保值不为null或对该空对象进行检查(if else).
Why don''t you just debug the code to find out which value is null and then either make sure that that value is not null or put in a check (if else) for that null object.


这篇关于在c#.net中创建新的objet引用null异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆