执行代码时显示错误...对象引用未设置为对象实例... [英] showing error while executing code...object reference not set to instance of an object...

查看:67
本文介绍了执行代码时显示错误...对象引用未设置为对象实例...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

obj.firstName = TextBox1.Text;
            obj.lastname = TextBox2.Text;
            obj.email = TextBox4.Text;
            obj.newrecruit = TextBox5.Text;
            obj.site.siteid = Convert.ToInt32(DropDownList4.SelectedValue);
            obj.manageothers = DropDownList1.SelectedValue;
            obj.topfunction.topfunctionid = Convert.ToInt32(DropDownList2.SelectedValue);
            obj.subfunction.subfunctionid = Convert.ToInt32(DropDownList3.SelectedValue);
            obj.lmsrole.lmsroleid = Convert.ToInt32(DropDownList5.SelectedValue);
            obj.channel.channelid = Convert.ToInt32(DropDownList6.SelectedValue);
            obj.employmentstatus.employmentstatusid = Convert.ToInt32(DropDownList7.SelectedValue);
            obj.status.statusid = Convert.ToInt32(DropDownList8.SelectedValue);
            obj.active = TextBox14.Text;
            obj.comments = TextBox15.Text;
            obj.localjobtitle = TextBox16.Text;
            obj.locallanguagejobtitle = TextBox17.Text;
            obj.kmlevel = Convert.ToInt32(DropDownList9.SelectedValue);
            br.viewdetails(obj);




错误出现在第四行...这是siteid ....这是向数据库添加详细信息的代码...此代码在添加按钮单击事件中...任何人都可以告诉为什么此错误发生乍一看,SelectedValue值之一为空,因为未选择任何内容.至少,这是最可能的原因.您应该始终将其检查为null,而不是假设始终选择了某些DropDownList项目.

原则上,如果还存在其他问题,即使您objbr也不显示这些对象的实例化位置,但这也不太可能.

让我告诉你,问这样的问题是不好的.您所看到的实际上是一个例外.输入您的问题:例外".编写确切的异常完全限定的类型名称和消息,并在抛出该行的位置写上确切的注释.使用调试器或捕获线程堆栈顶部的所有异常并将其转储.有关代码行号,请检查/输出Exception.Stack.

这些是基本的调试和问题报告技术,您确实需要非常了解和使用它们,否则您就不能说自己开发了任何东西.目前,您还没有发展.您只是想踢一台电视机,直到它开始显示图片.它不会让你走远.

—SA


obj.firstName = TextBox1.Text;
            obj.lastname = TextBox2.Text;
            obj.email = TextBox4.Text;
            obj.newrecruit = TextBox5.Text;
            obj.site.siteid = Convert.ToInt32(DropDownList4.SelectedValue);
            obj.manageothers = DropDownList1.SelectedValue;
            obj.topfunction.topfunctionid = Convert.ToInt32(DropDownList2.SelectedValue);
            obj.subfunction.subfunctionid = Convert.ToInt32(DropDownList3.SelectedValue);
            obj.lmsrole.lmsroleid = Convert.ToInt32(DropDownList5.SelectedValue);
            obj.channel.channelid = Convert.ToInt32(DropDownList6.SelectedValue);
            obj.employmentstatus.employmentstatusid = Convert.ToInt32(DropDownList7.SelectedValue);
            obj.status.statusid = Convert.ToInt32(DropDownList8.SelectedValue);
            obj.active = TextBox14.Text;
            obj.comments = TextBox15.Text;
            obj.localjobtitle = TextBox16.Text;
            obj.locallanguagejobtitle = TextBox17.Text;
            obj.kmlevel = Convert.ToInt32(DropDownList9.SelectedValue);
            br.viewdetails(obj);




error is coming in the 4th line...that is siteid ....this is the code to add details to database...this code is in the add button click event...can anyone plss tell why this error is coming?

解决方案

From the first glance, one of SelectedValue values is null, because nothing is selected. At least, this is the most likely reason. You should always check it for null, not assuming that some DropDownList item is always selected.

In principle, if could be something else as well, even you obj or br as you don''t show where these objects are instantiated, but this is less likely.

Let me tell you that asking a question like that is not nice. What you see is really an exception. Write in your question: "exception". Write exact exception fully-qualified type name and message and comment exact line where it was thrown. Use the debugger or catch all exceptions on top of stack of your thread and dump it. For code line numbers, inspect/output Exception.Stack.

These are elementary debugging and issue reporting technique, which you really need to know and use very well, otherwise you cannot really say that you develop anything. At this moment, you are not developing yet; you are just trying to kick a TV set until it start showing the picture. It won''t get you far.

—SA


这篇关于执行代码时显示错误...对象引用未设置为对象实例...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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