报错:System.NullReferenceException: 未将对象引用设置到对象的实例。

本人小白,想实现将网页的表写入到word中并导出到本地。但是总是报错“System.NullReferenceException: 未将对象引用设置到对象的实例。
代码部分如下


 protected void docWriteIn2(string TemplatePath)
        {
            Aspose.Words.Document Doc = new Aspose.Words.Document(TemplatePath); //载入模板
            DocumentBuilder builder = new DocumentBuilder(Doc);
            NodeCollection tables = Doc.GetChildNodes(NodeType.Table, true);

            Aspose.Words.Tables.Table table1 = tables[0] as Aspose.Words.Tables.Table;


            builder.MoveToBookmark("QInspectionDepartId");
            builder.Write(QInspectionDepartId.Text.Trim());
            builder.MoveToBookmark("Number");
            builder.Write(Number.Text.Trim());
            builder.MoveToBookmark("QQuantity");
            builder.Write(QQuantity.Text.Trim());
            builder.MoveToBookmark("QualifiedQuantity");
            builder.Write(QualifiedQuantity.Text.Trim());
            builder.MoveToBookmark("UqualifiedQuantity");
            builder.Write(UqualifiedQuantity.Text.Trim());
            builder.MoveToBookmark("PassRate");
            builder.Write(PassRate.Text.Trim());

            //拿到第一个表格:tableindex=0
            Aspose.Words.Tables.Table table2 = tables[1] as Aspose.Words.Tables.Table;
            //拿到第二行的row模板:rowindex=1
            var roww = table2.Rows[1];
            if (Session["receiptNumber"]!=null)
            {
                var TestInfo = Testinfo.findTestInfo(Session["receiptNumber"].ToString());
**                QInspectionDepartId.Text = contextdb.S_Department_Info.Single(u => u.id == TestInfo.InspectionDepartId).DepartmentName;
**                              **//**这里肯定是有问题的**,问题出现在上面这一行****
            // 第二行QInspectionDepartId.Text = contextdb.S_Department_Info.Single(u => u.id == DepartId).DepartmentName;
            }
            var TestBasicInfoList = db.GetTable<K_QCM_TestBasicInfo>();
            var TargetListInfoList = db.GetTable<K_QCM_Target_List>();
            var node_table = db.GetTable<K_QCM_Score_Tree>();
            var TestBasic = from t in TestBasicInfoList
                            where t.SourceNumber == Session["receiptNumber"].ToString()
                            select t;
            var list = edit2.findReceiptGiven(Session["receiptNumber"].ToString());
            var DetailInfoList = db.GetTable<K_W_Receipt_DetailInfo>();
            var ReceiptDetail = from t in DetailInfoList
                               where t.ReceiptNumber == Session["receiptNumber"].ToString()
                               select t;
            int n = 0;
            foreach (var s in TestBasic)
            {
                var TargetDetail = from t in TargetListInfoList
                                   where t.ProductNumber == s.InspectionNumber
                                   select t;

                foreach (var i in TargetDetail)
                {
                    //Button Button1 = new Button();
                    //Button1.Text = "按钮";
                    //Button1.ID = i.id.ToString();
                    //Button1.Click += new System.EventHandler(Button1_Click);
                    int nodeId = db.K_QCM_Score_Tree.Single(u => u.ParentId == 0 && u.SchemaId == i.SchemaId).id;
                    float value = float.Parse(db.K_QCM_Score_Value.Single(u => u.TargetId == i.id && u.NodeId == nodeId).Value.ToString());
                    K_QCM_Score_Tree selected_node = node_table.Single(h => h.id == nodeId);
                    if (value == 0)
                    {
                        K_P_Product product = edit.findProductFromCoding(i.ProductCode);

                        //复制第2行
                        var row = roww.Clone(true);
                        //将复制的行插入当前行的上方
                        table2.Rows.Insert(1 + n, row);

                        //光标移动到:第一个表格的,第1行的第1列的格子里
                        builder.MoveToCell(1, n + 1, 0, 0);
                        //给序号填充内容
                        builder.Write((n + 1).ToString());

                        //给产品名称单元格填充内容
                        builder.MoveToCell(1, 1 + n, 1, 0);
                        builder.Write(i.ProductName);

                        //给规格型号填充内容
                        builder.MoveToCell(1, 1 + n, 2, 0);
                        builder.Write(product.Norm);

                        //给单位填充内容
                        builder.MoveToCell(1, 1 + n, 3, 0);
                        builder.Write(product.Unit);

                        //给数量填充内容
                        builder.MoveToCell(1, 1 + n, 4, 0);
                        builder.Write("1");

                        //
                        builder.MoveToCell(1, 1 + n, 5, 0);
                        builder.Write("不合格");
                        //总计
                    }
                    else
                    {
                        K_P_Product product = edit.findProductFromCoding(i.ProductCode);
                        //复制第2行
                        var row = roww.Clone(true);
                        //将复制的行插入当前行的上方
                        table2.Rows.Insert(1 + n, row);

                        //光标移动到:第一个表格的,第1行的第1列的格子里
                        builder.MoveToCell(1, n + 1, 0, 0);
                        //给序号填充内容
                        builder.Write((n + 1).ToString());

                        //给产品名称单元格填充内容
                        builder.MoveToCell(1, 1 + n, 1, 0);
                        builder.Write(i.ProductName);

                        //给规格型号填充内容
                        builder.MoveToCell(1, 1 + n, 2, 0);
                        builder.Write(product.Norm);

                        //给单位填充内容
                        builder.MoveToCell(1, 1 + n, 3, 0);
                        builder.Write(product.Unit);

                        //给数量填充内容
                        builder.MoveToCell(1, 1 + n, 4, 0);
                        builder.Write("1");

                        //检验结果
                        builder.MoveToCell(1, 1 + n, 5, 0);
                        builder.Write("合格");
                    }
                    n = n + 1;

                }
            }
            string docName = "word文档";



            Doc.Save(docName + ".doc", Aspose.Words.SaveFormat.Doc, SaveType.OpenInWord, Response);



        }

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

异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。
有人知道怎么解决吗?

2个回答

看哪一行代码报错,再看哪一个变量是null,想办法让它不要null

                var TestInfo = Testinfo.findTestInfo(Session["receiptNumber"].ToString());
**                QInspectionDepartId.Text = contextdb.S_Department_Info.Single(u => u.id == TestInfo.InspectionDepartId).DepartmentName;

可能是你查询出来没数据,然后你又使用了它的属性

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问