大家好,我在一个DrawGraph类中写一个画图程序,代码如下:
 string imgStorePath = "\\Image\\" + userID + "\\Weight_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".jpg";
 btp.Save(imgStorePath);我用跟踪测试时,发现执行到Save时出错,提示“未将对象引用设置到对象的实例”,但imgStorePath是有正确的值。是不能在类中这样处理,还是什么原因?100分求救,高手指点下,多谢。

解决方案 »

  1.   

    imgStorePath 必需是Server.MapPath
      

  2.   

    +1
    必须绝对path。
    论坛签名======================================================================success1000:你好!
    截至 2011-10-28 17:45:38 前:
    你已发帖 16 个, 未结贴 1 个;
    结贴率为: 93.75%

    当您的问题得到解答后请及时结贴.

    http://topic.csdn.net/u/20090501/15/7548d251-aec2-4975-a9bf-ca09a5551ba5.html
    http://topic.csdn.net/u/20100428/09/BC9E0908-F250-42A6-8765-B50A82FE186A.html
    http://topic.csdn.net/u/20100626/09/f35a4763-4b59-49c3-8061-d48fdbc29561.html如何给分和结贴?
    http://community.csdn.net/Help/HelpCenter.htm#结帖如何给自己的回帖中也加上签名?
    http://blog.csdn.net/q107770540/archive/2011/03/15/6250007.aspx
      

  3.   

    我是在类中写的,好像不能用“Server.”,是什么原因,缺少什么引用?
      

  4.   

    需要使用:System.Web.HttpContext.Current.Server.MapPath()
      

  5.   

    层层获取,修改一下就可以 /// <summary>
      /// 根据从数据库中提取出来的文件路径,分离出在客户端头像的路径
      /// </summary>
      /// <param name="fileName">数据库中提取出来的文件路径</param>
      /// <param name="faceFlag">是否进入客户端头像目录下</param>
      /// <returns>faceFalg为True时,返回头像目录下的头像全路径,当faceFalg为False时,返回用户ID目录下的头像全路径 </returns>
      public string getImgPath(string fileName, bool faceFlag)
      {
      int place;
      string comPath = Application.StartupPath;
      for (int i = 0; i < 2; i++)
      {
      place = comPath.LastIndexOf("\\");
      comPath = comPath.Remove(place);
      }
      if (!faceFlag)
      {
      if (Directory.Exists(comPath + @"\\" + myInfo.Key.ToString()))
      {
      comPath = comPath + @"\\" + myInfo.Key.ToString() + fileName;
      }
      else
      {
      Directory.CreateDirectory(comPath + @"\\" + myInfo.Key.ToString());
      comPath = comPath + @"\\" + myInfo.Key.ToString() + fileName;
      }
      }
      else if (faceFlag)
      {
      comPath = comPath + @"\NEWFACE\" + fileName;
      }
      return comPath;
      }
      

  6.   

    System.Web.HttpContext.Current.Server.MapPath(".");
      

  7.   

    用了Server.MapPath,但还是提示“未将对象引用设置到对象的实例”。
      

  8.   

    string sPath= "\\Image\\" + userID + ";
    if (!Directory.Exists(sPath))
    {
         Directory.CreateDirectory(sPath);
    }
    string imgStorePath =sPath+\\Weight_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".jpg";
     btp.Save(imgStorePath);
      

  9.   


    //获取跟目录下面的images文件夹,你得用到这个方法Server.MapPath
    private string fullpath = HttpContext.Current.Server.MapPath("~/images/");
      

  10.   

    btp.Save(imgStorePath);是不是btp没有初始化???
      

  11.   

    前台里面是否是filetype属性忘了设置了!~
      

  12.   

    很明显的路径问题:给你段,图片上传的代码,希望帮到你:
    string fileName = wCacheRp.GetRandomStr(8) + fileExtension;
      imgname.Text = fileName;
      string fn = Server.MapPath(Request.ApplicationPath) + "\\icon\\" + fileName;
      imgUp.SaveAs(fn);
      

  13.   

    为什么那么多人要纠结路径问题,明显是btp为空,路径不对的话,就不是这个错误了。
      

  14.   

    太多人纠结与路径,的确是btp为空的问题。哈~
      

  15.   

    btp.Save(imgStorePath);改btp.Save(server.MapPath(imgStorePath));