[已解决]在保存附件功能中,它是否将对象引用设置为未设置为对象的实例? [英] [Solved] In save attachment function it is giving object refernce not set to instance of object ?

查看:82
本文介绍了[已解决]在保存附件功能中,它是否将对象引用设置为未设置为对象的实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

protected void SaveAttachments(Int32 actRuleID)
    {
        if (gvAttachments.Rows.Count > 0)
        {
            if ((Session["FileDetail"].ToString() != ""))
            {
                string[] Records = Session["FileDetail"].ToString().Split('#');
                byte[] fileContent;
                FileStream fs;
                BinaryReader br;
                for (int i = 0; i < Records.Length; i++)
                {

                    string[] arrRecords = Records[i].Split(',');

                    fs = new FileStream(arrRecords[2], FileMode.Open, FileAccess.Read);
                    br = new BinaryReader(fs);
                    fileContent = br.ReadBytes((int)fs.Length);
                    br.Close();
                    fs.Close();
                    Econnect.EEMS.ActRulesAttachments objAttachment = new Econnect.EEMS.ActRulesAttachments();
                    objAttachment.ActRuleID = actRuleID;
                    objAttachment.FileName = arrRecords[0].ToString();
                    objAttachment.FileFormat = arrRecords[3].ToString();
                    objAttachment.FileContent = fileContent;
                    context.ActRuleAttachments.Add(objAttachment);
                    context.SaveChanges();
                }
            }

        }
    }

推荐答案

1。发生此错误的原因是您尝试访问 null 对象的属性/方法。



2.您应该调试代码并在代码中检查生成异常的已使用变量的值,其中一个应为null。



PS:通常当您从Session中读取内容时,如果 null ,则应测试它。 if(Session [FileDetail]!= null&& Session [FileDetail]。ToString()!=)){...}
1.This error occurs because you are trying to access the property/method of a null object.

2.You should Debug your code and inspect in your code that generate the exception the values of the used variables, and one of it should be null.

PS: Normally when you read something from Session you should test it if is null or not. if(Session["FileDetail"] != null && Session["FileDetail"].ToString() != "")){...}


解决方案1的附加内容,请检查变量。也许他们没有生成一个值,这就是为什么你得到一个例外。



如果您使用的是Visual Studio,请使用断点以便您可以看到具体的内容你遇到问题。
Additional to the Solution 1, kindly check for the variables. Maybe they don't generate a value that's why you're getting an exception.

If you are using Visual Studio, use breakpoint so that you can see in what specific line you are having problems with.


这篇关于[已解决]在保存附件功能中,它是否将对象引用设置为未设置为对象的实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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