I have come across several issues with Graphic Element creation in ArcGIS Pro SDK 2.5 - some of them have been in before although they have changed slightly at 2.5. I'll make a question for each one.
If I create and then set Name on many graphic elements whilst the layout is open then it crashes ArcGIS Pro - if I do it whilst the layout is closed then it's OK.
An unhandled exception of type 'System.NullReferenceException' occurred in ArcGIS.Desktop.Layouts.dll
Additional information: Object reference not set to an instance of an object.
Layout layout <SPAN class="operator token">=</SPAN> Project<SPAN class="punctuation token">.</SPAN>Current<SPAN class="punctuation token">.</SPAN>GetItems<SPAN class="operator token"><</SPAN>LayoutProjectItem<SPAN class="operator token">></SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="token function">FirstOrDefault</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="operator token">?</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="token function">GetLayout</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="keyword token">for</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="keyword token">int</SPAN> i <SPAN class="operator token">=</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">;</SPAN> i <SPAN class="operator token"><</SPAN> <SPAN class="number token">100</SPAN><SPAN class="punctuation token">;</SPAN> i<SPAN class="operator token">++</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="punctuation token">{</SPAN>
List<SPAN class="operator token"><</SPAN>Coordinate2D<SPAN class="operator token">></SPAN> lineCoordinates <SPAN class="operator token">=</SPAN> <SPAN class="keyword token">new</SPAN> <SPAN class="token class-name">List</SPAN><SPAN class="operator token"><</SPAN>Coordinate2D<SPAN class="operator token">></SPAN> <SPAN class="punctuation token">{</SPAN> <SPAN class="keyword token">new</SPAN> <SPAN class="token class-name">Coordinate2D</SPAN> <SPAN class="punctuation token">{</SPAN> X <SPAN class="operator token">=</SPAN> i<SPAN class="punctuation token">,</SPAN> Y <SPAN class="operator token">=</SPAN> <SPAN class="number token">0</SPAN> <SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="keyword token">new</SPAN> <SPAN class="token class-name">Coordinate2D</SPAN> <SPAN class="punctuation token">{</SPAN> X <SPAN class="operator token">=</SPAN> i<SPAN class="punctuation token">,</SPAN> Y <SPAN class="operator token">=</SPAN> <SPAN class="number token">100</SPAN> <SPAN class="punctuation token">}</SPAN> <SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">;</SPAN>
Polyline polylineTic <SPAN class="operator token">=</SPAN> PolylineBuilder<SPAN class="punctuation token">.</SPAN><SPAN class="token function">CreatePolyline</SPAN><SPAN class="punctuation token">(</SPAN>lineCoordinates<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
CIMLineSymbol lineSym <SPAN class="operator token">=</SPAN> SymbolFactory<SPAN class="punctuation token">.</SPAN>Instance<SPAN class="punctuation token">.</SPAN><SPAN class="token function">ConstructLineSymbol</SPAN><SPAN class="punctuation token">(</SPAN>ColorFactory<SPAN class="punctuation token">.</SPAN>Instance<SPAN class="punctuation token">.</SPAN>BlackRGB<SPAN class="punctuation token">,</SPAN> <SPAN class="number token">1.0</SPAN><SPAN class="punctuation token">,</SPAN> SimpleLineStyle<SPAN class="punctuation token">.</SPAN>Solid<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
GraphicElement lineElm <SPAN class="operator token">=</SPAN> LayoutElementFactory<SPAN class="punctuation token">.</SPAN>Instance<SPAN class="punctuation token">.</SPAN><SPAN class="token function">CreateLineGraphicElement</SPAN><SPAN class="punctuation token">(</SPAN>layout<SPAN class="punctuation token">,</SPAN> polylineTic<SPAN class="punctuation token">,</SPAN> lineSym<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
CIMElement cimElement <SPAN class="operator token">=</SPAN> lineElm<SPAN class="punctuation token">.</SPAN><SPAN class="token function">GetDefinition</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
cimElement<SPAN class="punctuation token">.</SPAN>Name <SPAN class="operator token">=</SPAN> <SPAN class="string token">"Line"</SPAN> <SPAN class="operator token">+</SPAN> i<SPAN class="punctuation token">;</SPAN>
lineElm<SPAN class="punctuation token">.</SPAN><SPAN class="token function">SetDefinition</SPAN><SPAN class="punctuation token">(</SPAN>cimElement<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="punctuation token">}</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>Of course I can close the layout do the work and then reopen...
Adam