直接访问图片出错

 http://localhost:1966/Web/Images/BookCovers/5427.jpg

“/Web”应用程序中的服务器错误。
--------------------------------------------------------------------------------

未将对象引用设置到对象的实例。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。

源错误:


行 20:     {
行 21:         //获取路径+ISBN的字符串
行 22:         string path = context.Request.MapPath(COVERSADDR + context.Request.Params["ISBN"].ToString());
行 23:         //判断新图片是否存在
行 24:         if (!File.Exists(path + "_new.jpg"))
 

源文件: d:/zy/MyBookShop1117/MyBookShop/Web/App_Code/CoverHandler.cs    行: 22

堆栈跟踪:


[NullReferenceException: 未将对象引用设置到对象的实例。]
   CoverHandler.ProcessRequest(HttpContext context) in d:/zy/MyBookShop1117/MyBookShop/Web/App_Code/CoverHandler.cs:22
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +358
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +64


******************************************************
解决方法:
******************************************************
"未将对象引用设置到对象的实例"异常的原因
1、ViewState 对象为Null。
2、DateSet 空。
3、sql语句或Datebase的原因导致DataReader空。
4、声明字符串变量时未赋空值就应用变量。
5、未用new初始化对象。
6、Session对象为空。
7、对控件赋文本值时,值不存在
8、使用Request.QueryString()时,所获取的对象不存在,或在值为空时未赋初始值
9、使用FindControl时,控件不存在却没有做预处理
10、重复定义造成未将对象引用设置到对象的实例错误.
——————————
*************************************
控件冲突?
*************************************
protected void btnAdd_Click(object sender, EventArgs e)
    {
        try
        {
            string ImgPath = this.FileUpload1.PostedFile.FileName;
            string ImgName = ImgPath.Substring(ImgPath.LastIndexOf("//") + 1);
            string ImgExtend = ImgPath.Substring(ImgPath.LastIndexOf(".") + 1);
            if (!(ImgExtend == "bmp" | | ImgExtend == "jpg" | | ImgExtend == "gif" | | ImgExtend == "BMP" | | ImgExtend == "JPG" | | ImgExtend == "GIF"))
            {
              ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "alert", "alert('上传图片的格式不正确!!!');", true);
                return;
            }
            else
            {
                //判断文件大小是否大于4MB
                if (this.FileUpload1.PostedFile.ContentLength > 4000000)
                {
                    //Response.Write(" <script language='javascript'>alert('文件超过4MB,请重新选择!') </script>");
                    ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "alert", "alert('文件超过4MB,请重新选择!');", true);
                }
                else
                {
                    int FileLen = this.FileUpload1.PostedFile.ContentLength;
                    Byte[] FileData = new Byte[FileLen];
                    HttpPostedFile hp = FileUpload1.PostedFile;//创建访问客户端上传文件的对象
                    Stream sr = hp.InputStream;//创建数据流对象
                    sr.Read(FileData, 0, FileLen);//将图片数据放到FileData数组对象实例中,其中0代表数组指针的起始位置,FileLen表示要读取流的长度(指针的结素位置)
                    //调用存储过程插入值
                    SqlConnection conn = new SqlConnection(sqlconn);
                    SqlCommand cmd = conn.CreateCommand();
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "SucceeCaseInsert";
                    cmd.Parameters.AddWithValue("@SetupType",this.ddlType.Text.Trim());
                    cmd.Parameters.AddWithValue("@SetupName",this.txtSetupName.Text.Trim());
                    cmd.Parameters.AddWithValue("@ItemName", this.txtItemName.Text.Trim());
                    cmd.Parameters.AddWithValue("@ItemSynopsis",this.Content.Value.Trim());
                    cmd.Parameters.AddWithValue("@LinkSrc", this.txtLink.Text.Trim());
                    cmd.Parameters.AddWithValue("@PicName", ImgName);
                    cmd.Parameters.AddWithValue("@PicSrc", FileData);
                    cmd.Parameters.AddWithValue("@UploadUserName", "Admin");
                    cmd.Parameters.AddWithValue("@Remark", txtRemark.Text);
                    SqlParameter paramOut = cmd.Parameters.AddWithValue("@RETURN_VALUE", "");
                    paramOut.Direction = ParameterDirection.ReturnValue;

                    try
                    {
                        conn.Open();
                        cmd.ExecuteNonQuery();
                        conn.Close();
                        int res = (int)cmd.Parameters["@RETURN_VALUE"].Value;
                        if (res == 1)
                        {
                            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "alert", "alert('案例信息已经插入成功!!!');", true);
                            this.ddlType.Text = this.ddlType.Items[0].ToString();
                            this.txtSetupName.Text = "";
                            this.txtItemName.Text = "";
                            this.Content.Value = "";
                            this.txtLink.Text = "";
                            //this.FileUpload.FileName = "";
                            this.txtRemark.Text = "";
                        }
                        else
                        {
                            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "alert", "alert('案例信息类型已经存在!请重新输入!!');", true);
                        }

                    }
                    catch
                    {

                        ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "alert", "alert('程序内部错误,操作失败!!!');", true);

                    }
                }
            }
        }
        catch
        {
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "alert", "alert('程序内部错误,操作失败!!!');", true);
        }
    }
 
 
------------------------------------
报错的是"  string  ImgPath  =  this.FileUpload1.PostedFile.FileName;
"这一句
 
请大家赐教!!!!!!!!!!!!!!!
 
------------------------------------
SERVER.mappath( ImgPath  )
单步执行报这个错误吗
 
------------------------------------
SERVER.mappath(this.FileUpload1.PostedFile.FileName)
------------------------------------
报错的是"    string      ImgPath      =      this.FileUpload1.PostedFile.FileName; 
"这一句
------------------------------------
一般是先判断再取值
 
------------------------------------
我已经解决了啊
  谢谢你们 咯!
 
------------------------------------
但是不是这个
    还是得谢谢你!
 
------------------------------------
是控件冲突!!!!!!!!!!!!!!
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值