的NullReferenceException [英] NullReferenceException

查看:79
本文介绍了的NullReferenceException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有代码


row = tables [8] .Rows.Add(); //我已经参考

                        cells = row.Cells;                        

                        cells [6] .Range.Text = string.Format(" {0:f3}",emission);

                        cells [7] .Range.Text = string.Format(" {0:f3}",normEmission);

                        var rows = tables [8] .Rows;

                        tables [8] .Cell(rows.Count,1).Select();

                        application.Selection.MoveUp(WdUnits.wdLine,1,WdMovementType.wdExtend);

                        application.Selection.Cells.Merge();

                        row = tables [8] .Rows.Add(); //但是我为空为什么????

                        row = tables [8] .Rows.Add();

                     
 细胞= row.Cells; // Exeption 类型 'System.NullReferenceException'

的未处理的异常为什么有&的的NullReferenceException ???

解决方案

嗨Ahlompys,


当表格有垂直合并的单元格时,我可以重现你的问题,行对象为空。 


解决方法,我建议你遍历表格单元并通过最后一行访问单元格它是rowindex。  以下是将最后一行单元格对象放入列表的示例。

 List< Word.Cell> lstCell = new List< Cell>(); 
foreach(表格中的单元格[1] .Range.Cells)
            {
               如果(cell.RowIndex ==表[1] .Rows.Count)
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; {
                    lstCell.Add(细胞);&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;}
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; }


最好的问候,


< p style ="margin:0px"> Celeste


I have code

row = tables[8].Rows.Add();//This I have referense
                        cells = row.Cells;                        
                        cells[6].Range.Text = string.Format("{0:f3}", emission);
                        cells[7].Range.Text = string.Format("{0:f3}", normEmission);
                        var rows = tables[8].Rows;
                        tables[8].Cell(rows.Count, 1).Select();
                        application.Selection.MoveUp(WdUnits.wdLine, 1, WdMovementType.wdExtend);
                        application.Selection.Cells.Merge();
                        row = tables[8].Rows.Add();//BUT THIS I have null WHY????
                        row = tables[8].Rows.Add();
                        cells = row.Cells;//Exeption An unhandled exception of type 'System.NullReferenceException'

Why I have NullReferenceException???

解决方案

Hi Ahlompys,

When the table has vertically merged cells, I could reproduce your issue, the row object is null. 

To work around, I suggest you iterate through table cells and access the last row cells via it's rowindex.  Here is the example for getting  last row cells object into a list.

List<Word.Cell> lstCell=new List<Cell>();
foreach (Cell cell in tables[1].Range.Cells)
            {
                if (cell.RowIndex == tables[1].Rows.Count)
                {
                    lstCell.Add(cell);                                                     
                }
            }

Best Regards,

Celeste


这篇关于的NullReferenceException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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