CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
不看会后悔的Windows XP之经验谈 简单快捷DIY实用家庭影院
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  .NET技术 >  C#

Object reference not set to an instance of an object

楼主zsb78(killer)2005-02-25 15:59:25 在 .NET技术 / C# 提问

****源代碼  
      int   id=0;  
   
  SqlCommand   objcmd   =   new   SqlCommand("spcheckmprno",   objcon);  
  objcmd.CommandType   =   CommandType.StoredProcedure;  
  SqlParameter   objparam=new   SqlParameter("@mprno",SqlDbType.Char);  
  objparam.Value   =   mprno;  
  objcmd.Parameters.Add(objparam);  
  try  
  {  
  objcon.Open();  
  id   =   Convert.ToInt16(objcmd.ExecuteScalar().ToString());  
  objcon.Close();  
  }  
  catch   (Exception   e)  
  {  
  throw   e;  
  }  
   
   
   
   
  ***出錯:  
  Object   reference   not   set   to   an   instance   of   an   object.    
  Description:   An   unhandled   exception   occurred   during   the   execution   of   the   current   web   request.   Please   review   the   stack   trace   for   more   information   about   the   error   and   where   it   originated   in   the   code.    
   
  Exception   Details:   System.NullReferenceException:   Object   reference   not   set   to   an   instance   of   an   object.  
   
  Source   Error:    
   
   
  Line   91:   catch   (Exception   e)  
  Line   92:   {  
  Line   93:   throw   e;  
  Line   94:   }  
  Line   95:   if   (id   ==   0)  
     
   
   
  ***刪除這行後就可以  
  //id   =   Convert.ToInt16(objcmd.ExecuteScalar().ToString());  
  可能是返回的值為NULL就出錯  
  有什麼辦法解決呀  
  问题点数:20、回复次数:4Top

1 楼zsb78(killer)回复于 2005-02-25 16:21:47 得分 0

有值就不會出錯  
  如何處理空值呀Top

2 楼xixigongzhu(夕夕公主(※快乐在于自己※))回复于 2005-02-25 16:40:31 得分 20

string   str   =   Convert.ToString(objcmd.ExecuteScalar());  
  if   (str   !=   "")//这样没有值或者值为空的两种情况都考虑了  
  id   =   Convert.ToInt16(str);Top

3 楼xixigongzhu(夕夕公主(※快乐在于自己※))回复于 2005-02-25 16:41:49 得分 0

Convert.ToString方法如果传入参数为Null,是不会出错的,返回空字符串。Top

4 楼xixigongzhu(夕夕公主(※快乐在于自己※))回复于 2005-02-25 16:42:49 得分 0

但Convert.ToInt16方法的参数如果为Null或者空字符串是会出错的。Top

相关问题

  • Object reference not set to an instance of an object.
  • Object reference not set to an instance of an object.
  • error Message:Object reference not set to an instance of an object.
  • "Object reference not set to an instance of an object":奇怪的错误
  • 高分求:Object reference not set to an instance of an object
  • object reference not set to an instance of an object是什麽錯誤?
  • Object reference not set to an instance of an object.一般是什么原因造成?
  • 我安装OSmartDeviceFramework14的时候他说bject reference not set to an instance of an object
  • Page_Load中出现的问题,出现这个错误:Object reference not set to an instance of an object
  • 请教 :修改datagridview的数据 更新到数据库 代码如下 (object reference not set an instance of an object? )

关键词

  • objcmd
  • objcon
  • objparam
  • toint16
  • convert
  • line
  • reference not set
  • object
  • exception
  • to an instance

得分解答快速导航

  • 帖主:zsb78
  • xixigongzhu

相关链接

  • CSDN .NET频道
  • .NET类图书
  • C#类图书
  • .NET类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo