首页 新闻 会员 周边 捐助

Object reference not set to an instance of an object. (未将对象引用到实例)

-1
悬赏园豆:50 [已解决问题] 解决于 2010-08-02 16:57

在将对象序列化的时候,本地测试没问题。在iis6下就出现Object reference not set to an instance of an object. 请大虾们帮忙看看.

代码如下:

protected void btnsubmit_Click(object sender, EventArgs e)
        {

            Jisuangongshi jisuangongs = new Jisuangongshi();

            jisuangongs.Zbgs = txtzb.Text;
            jisuangongs.Djgs = txtdj.Text;
            jisuangongs.Jbgs = txtjb.Text;

            save(jisuangongs);
        }

        public void save(Jisuangongshi lstjsuan)
        {
            FileStream filestream = null;
            try
            {
               
                filestream = new FileStream("ProduceCheck.files", FileMode.Create);
                BinaryFormatter bf = new BinaryFormatter();
                bf.Serialize(filestream, lstjsuan);
            }
            catch (Exception ex)
            {
               
            }
            finally
            {
                filestream.Close();
            }
           
            Response.Write("<script>alert('修改成功!');window.location.href='ParaManager.aspx';</script>");
        }

[Serializable]
    public class Jisuangongshi
    {
        private string _djgs;

        public string Djgs
        {
            get { return _djgs; }
            set { _djgs = value; }
        }

        private string _zbgs;

        public string Zbgs
        {
            get { return _zbgs; }
            set { _zbgs = value; }
        }

        private string _jbgs;

        public string Jbgs
        {
            get { return _jbgs; }
            set { _jbgs = value; }
        }

问题补充: Server Error in '/' Application. -------------------------------------------------------------------------------- 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 53: finally Line 54: { Line 55: filestream.Close(); Line 56: } Line 57: Source File: E:\ProduceCheck\ProduceCheck.Web\CompanyManager.aspx.cs Line: 55 Stack Trace: [NullReferenceException: Object reference not set to an instance of an object.] ProduceCheck.Web.CompanyManager.save(Jisuangongshi lstjsuan) in E:\ProduceCheck\ProduceCheck.Web\CompanyManager.aspx.cs:55 ProduceCheck.Web.CompanyManager.btnsubmit_Click(Object sender, EventArgs e) in E:\ProduceCheck\ProduceCheck.Web\CompanyManager.aspx.cs:36 System.Web.UI.WebControls.Button.OnClick(EventArgs e) +105 System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +107 System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5102
风吹落叶的主页 风吹落叶 | 初学一级 | 园豆:67
提问于:2010-07-29 16:44
< >
分享
最佳答案
1

filestream = new FileStream("ProduceCheck.files", FileMode.Create);

这里创建 FileStream 的时候抛出了异常,

你把异常吃掉了,

catch (Exception ex) 
{
      throw ex; // 这里出现的异常会告诉你你无权县创建FileStream,或者  ProduceCheck.files 已存在等错误.
}

所以,异常会从这里抛出,因为 filestream == null,系统报告 NullReferenceException.

finally
{
                filestream.Close();
}

收获园豆:50
Launcher | 高人七级 |园豆:45050 | 2010-07-30 10:16
一开始我也觉得权限问题,那要给什么权限。everyone,aspx.net,iuser都给了读写权限了。可是还是没用。按照你的做了。貌似还是这样子。
风吹落叶 | 园豆:67 (初学一级) | 2010-07-30 10:30
@风吹落叶: 你确定 filestream 不等于 null 了吗?
Launcher | 园豆:45050 (高人七级) | 2010-07-30 10:42
@Galactica:因为你将 FileStream 定义在 try 外部,因此在 finally 中一定要先判断 filestream 是否为空 if(filestream != null) filestream.Close();
Launcher | 园豆:45050 (高人七级) | 2010-07-30 10:46
恩~添加判断了。还是原来的样子。我在想~为什么本地没问题。iis6下就出现问题了。权限?还是?
风吹落叶 | 园豆:67 (初学一级) | 2010-07-30 11:11
@风吹落叶:你是说你添加判断后,还是调用了 filestream.Close() 吗?
Launcher | 园豆:45050 (高人七级) | 2010-07-30 11:52
如果你按照我说的做了,你收到的错误信息应该如下: “/”应用程序中的服务器错误。 -------------------------------------------------------------------------------- 对路径“C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ProduceCheck.files”的访问被拒绝。 说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.UnauthorizedAccessException: 对路径“C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ProduceCheck.files”的访问被拒绝。 ASP.NET 未被授权访问所请求的资源。请考虑授予 ASP.NET 请求标识访问此资源的权限。ASP.NET 有一个在应用程序没有模拟时使用的基进程标识(通常,在 IIS 5 上为 {MACHINE}\ASPNET,在 IIS 6 上为网络服务)。如果应用程序正在通过 <identity impersonate="true"/> 模拟,则标识将为匿名用户(通常为 IUSR_MACHINENAME)或经过身份验证的请求用户。 要将 ASP.NET 访问权限授予某个文件,请在资源管理器中右击该文件,选择“属性”,然后选择“安全”选项卡。单击“添加”添加适当的用户或组。突出显示 ASP.NET 帐户,选中所需访问权限对应的框。 源错误: 行 39: catch (Exception ex) 行 40: { 行 41: throw ex; 行 42: } 行 43: finally 源文件: D:\SOA\MSDXMApplication\WebApplication1\Default.aspx.cs 行: 41 堆栈跟踪: [UnauthorizedAccessException: 对路径“C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ProduceCheck.files”的访问被拒绝。] WebApplication1._Default.save(Jisuangongshi lstjsuan) in D:\SOA\MSDXMApplication\WebApplication1\Default.aspx.cs:41 WebApplication1._Default.btnsubmit_Click(Object sender, EventArgs e) in D:\SOA\MSDXMApplication\WebApplication1\Default.aspx.cs:27 System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111 System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110 System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565
Launcher | 园豆:45050 (高人七级) | 2010-07-30 12:04
当你能得到上面这样的错误信息的时候,我们再来说如何指定可写的目录,或者如何给拒绝被访问的目录增加写权限.
Launcher | 园豆:45050 (高人七级) | 2010-07-30 12:05
@Galactica:原先还是报原先的错,是因为dll没跟新。现在是 Server Error in '/' Application. -------------------------------------------------------------------------------- Access to the path 'c:\windows\system32\inetsrv\ProduceCheck.Web.files' is denied. 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.UnauthorizedAccessException: Access to the path 'c:\windows\system32\inetsrv\ProduceCheck.Web.files' is denied. ASP.NET is not authorized to access the requested resource. Consider granting access rights to the resource to the ASP.NET request identity. ASP.NET has a base process identity (typically {MACHINE}\ASPNET on IIS 5 or Network Service on IIS 6) that is used if the application is not impersonating. If the application is impersonating via <identity impersonate="true"/>, the identity will be the anonymous user (typically IUSR_MACHINENAME) or the authenticated request user. To grant ASP.NET access to a file, right-click the file in Explorer, choose "Properties" and select the Security tab. Click "Add" to add the appropriate user or group. Highlight the ASP.NET account, and check the boxes for the desired access. Source Error: Line 49: } Line 50: else Line 51: { Line 52: if (ParametersBLL.addPara(txtzb.Text, txtdj.Text, txtjb.Text)) Line 53: { Source File: D:\ProduceCheck\ProduceCheck.Web\CompanyManager.aspx.cs Line: 51 Stack Trace: [UnauthorizedAccessException: Access to the path 'c:\windows\system32\inetsrv\ProduceCheck.Web.files' is denied.] ProduceCheck.Web.CompanyManager.save(Jisuangongshi lstjsuan) in D:\ProduceCheck\ProduceCheck.Web\CompanyManager.aspx.cs:51 ProduceCheck.Web.CompanyManager.btnsubmit_Click(Object sender, EventArgs e) in D:\ProduceCh
风吹落叶 | 园豆:67 (初学一级) | 2010-08-02 11:37
@风吹落叶:一种方式就按照提示给权限,另一种方式就是把你的文件ProduceCheck.Web.Files移到别的目录,比如filestream = new FileStream(@" E:\ProduceCheck\ProduceCheck.Web.files", FileMode.Create);
Launcher | 园豆:45050 (高人七级) | 2010-08-02 12:56
其他回答(2)
1

至少说明哪一行报的错啊

Gray Zhang | 园豆:17610 (专家六级) | 2010-07-29 23:31
0

 遇到过一种情况是dll 和aspx页面不匹配造成的

亚洲DotNet首席技师 | 园豆:143 (初学一级) | 2015-05-15 15:07
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册