<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: System.NullReferenceException, Switching python code to c# language in Revit API Forum</title>
    <link>https://forums.autodesk.com/t5/revit-api-forum/system-nullreferenceexception-switching-python-code-to-c/m-p/10991259#M20409</link>
    <description>After placing your breakpoint, make sure Revit is running and select Attach to Process, then select Revit. After you've attached to Revit you can run your command and start debugging. Although, you'll need to have the latest build of your code in your addins folder in order for it to work correctly.</description>
    <pubDate>Tue, 08 Mar 2022 14:25:14 GMT</pubDate>
    <dc:creator>AGGilliam</dc:creator>
    <dc:date>2022-03-08T14:25:14Z</dc:date>
    <item>
      <title>System.NullReferenceException, Switching python code to c# language</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/system-nullreferenceexception-switching-python-code-to-c/m-p/10987741#M20400</link>
      <description>&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="MiguelGT17_0-1646624520820.png" style="width: 600px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/1033077iCF69BAA9354EBEE9/image-size/medium?v=v2&amp;amp;px=400" role="button" title="MiguelGT17_0-1646624520820.png" alt="MiguelGT17_0-1646624520820.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Greeting to all, when switching from python to c# code I'm facing a NullReferenceException. In fact, SharpDevelop builds the script successfully and the exception comes up when hitting the run button in macro manager. So far I've identified that the exception triggers in the code that is wrapped inside the if ...NumberOfSegments&amp;gt;1 and if...NumberOfSegments==1:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="MiguelGT17_1-1646626106229.png" style="width: 765px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/1033083i21C769AEE0D80166/image-dimensions/765x399?v=v2" width="765" height="399" role="button" title="MiguelGT17_1-1646626106229.png" alt="MiguelGT17_1-1646626106229.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Any thoughts on how to get this fixed?&lt;/P&gt;&lt;P&gt;Best regards&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;Transaction tr2=new Transaction(doc,"AFRY:Multi-Rebar Alignment");
tr2.Start();

view= doc.ActiveView;
cropbox=view.CropBox;
transform=cropbox.Transform;
XYZ bbMin=cropbox.Min;
XYZ bbMax=cropbox.Max;
Dictionary&amp;lt;int, Element&amp;gt; unTagged= new Dictionary&amp;lt;int, Element&amp;gt;(), rebTagged=new Dictionary&amp;lt;int, Element&amp;gt;();
Dictionary&amp;lt;int, XYZ&amp;gt; Top= new Dictionary&amp;lt;int, XYZ&amp;gt;(), Bottom=new Dictionary&amp;lt;int, XYZ&amp;gt;(), Right= new Dictionary&amp;lt;int, XYZ&amp;gt;(), Left=new Dictionary&amp;lt;int, XYZ&amp;gt;();

if (familyType=="Section") 
{
		
	for (int i = 0; i &amp;lt; rebars.Count; i++) 
	{
		
		//a). rebar bounding box midpoint in 3d revit model system (it might retrieve a partial bounding box if the whole element is not visible)
		Element reb=rebars[i];
		
		if ((reb as Rebar).IsRebarFreeForm()== true)
		{
			unTagged.Add(i,reb);
		}
		else if ((reb as Rebar).IsRebarFreeForm()== false) 
		{
		
			
			BoundingBoxXYZ rebBB=reb.get_BoundingBox(view);
			XYZ midPoint=(rebBB.Max + rebBB.Min)/2;
			
			//b). Transform the coord system into the local view coord system
			Transform inverseTf=transform.Inverse;
			XYZ rebLocInView=inverseTf.OfPoint(midPoint);
			
			//c) Determine the center point of the cropview w.r.t view system coord (using z=0)
			XYZ newMin=new XYZ(bbMin.X,bbMin.Y,0);
			XYZ newMax=new XYZ(bbMax.X,bbMax.Y,0);
			
			Curve line1=Line.CreateBound(newMax,newMin);
			XYZ centerPt=line1.Evaluate(0.5,true);
			
			//d) Get the direction of the distribution path from the rebar reinforcement
			//dissclosure: Free form rebars do not support getshapedrivenaccessor
			RebarShapeDrivenAccessor accessor=(reb as Rebar).GetShapeDrivenAccessor();
			XYZ dir=accessor.GetDistributionPath().Direction;
			
			//e).Get the curves of a typical rebar within the whole set
			IList&amp;lt;Curve&amp;gt; curves=accessor.ComputeDrivingCurves();
			
			//f).  filter rebars that have more than 1 segment (Null Reference from what is inside segments&amp;gt;1 or ==1!!)
			//try 
			//{

			if ( ((doc.GetElement((reb as Rebar).GetShapeId()) as RebarShape).GetRebarShapeDefinition() as RebarShapeDefinitionBySegments).NumberOfSegments &amp;gt;1 ) 
			{
				//f.1) Stirrups that follows a horizontal path direction: set try &amp;amp; catch for diagonal stirrup that follow hz dir
				if (Math.Round(Math.Abs(dir.X),4)== Math.Round(Math.Abs(view.RightDirection.X),4) &amp;amp;&amp;amp; Math.Round(Math.Abs(dir.Y),4)==Math.Round(Math.Abs(view.RightDirection.Y),4))
				{
					rebTagged.Add(i,reb);
					Left.Add(i,rebLocInView);
					
				}
				
				//f.2) Stirrups that follow a vertical path direction for rebars
				if (Math.Round(Math.Abs(dir.Z),4)==Math.Round(Math.Abs(view.UpDirection.Z),4))
				{
					rebTagged.Add(i,reb);
					Top.Add(i,rebLocInView);
				}
			}
				
			if ( ((doc.GetElement((reb as Rebar).GetShapeId()) as RebarShape).GetRebarShapeDefinition() as RebarShapeDefinitionBySegments).NumberOfSegments == 1 ) 
			{
				//H.1).ANY REBAR ORIENTATION ALONG THE viewDirection: untagged()
				if (Math.Round(Math.Abs(dir.X),4)== Math.Round(Math.Abs(view.ViewDirection.X),4) &amp;amp;&amp;amp; (Math.Round(Math.Abs(dir.Y),4)==Math.Round(Math.Abs(view.UpDirection.Y),4)) )
				{
					unTagged.Add(i, reb);
				}
				
				//H.2) straight Horizontal rebar along viewUpDirection (points along UpDir as well) --&amp;gt; This goes at Top or bottom
				if (Math.Round(Math.Abs(dir.X),4)== Math.Round(Math.Abs(view.UpDirection.X),4) &amp;amp;&amp;amp; (Math.Round(Math.Abs(dir.Y),4)==Math.Round((Math.Abs(view.UpDirection.Y)),4)) &amp;amp;&amp;amp; (Math.Round(Math.Abs((curves[0] as Line).Direction.Z)))==0 )
				{
					rebTagged.Add(i,reb);
					Top.Add(i,rebLocInView);
				}
				
				//H.3) straight vertical rebal along viewRight direction path
				if ( Math.Round(Math.Abs(dir.X),4) == Math.Round(Math.Abs(view.RightDirection.X),4) &amp;amp;&amp;amp; Math.Round(Math.Abs(dir.Y),4) == Math.Round(Math.Abs(view.RightDirection.Y),4) &amp;amp;&amp;amp; Math.Abs((curves[0] as Line).Direction.Z)==1)
				{
					
					rebTagged.Add(i,reb);
					Left.Add(i,rebLocInView);
					
				}
				
				
				// H.4) Horizontal rebars as points along the right side
				
				if (Math.Round(Math.Abs(dir.X),4) == Math.Round(Math.Abs(view.RightDirection.X),4) &amp;amp;&amp;amp; Math.Round(Math.Abs(dir.Y),4) == Math.Round(Math.Abs(view.RightDirection.Y),4) &amp;amp;&amp;amp; Math.Round(Math.Abs((curves[0] as Line).Direction.Z),4)==0 )
				{
					rebTagged.Add(i,reb);
					Left.Add(i,rebLocInView);
				}
			}	
			//} 
			//catch (Exception)
			//{
			//unTagged.Add(i,reb);
			//throw;
			//}
			

		}
	}
}
tr2.Commit();&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 07 Mar 2022 04:10:46 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/system-nullreferenceexception-switching-python-code-to-c/m-p/10987741#M20400</guid>
      <dc:creator>MiguelGT17</dc:creator>
      <dc:date>2022-03-07T04:10:46Z</dc:date>
    </item>
    <item>
      <title>Re: System.NullReferenceException, Switching python code to c# language</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/system-nullreferenceexception-switching-python-code-to-c/m-p/10989243#M20401</link>
      <description>&lt;P&gt;RebarShapeDefinition is the base class to:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;RebarShapeDefinitionBySegments&lt;BR /&gt;RebarShapeDefinitionByArc (I imagine this one is quite rare however).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So perhaps your 'As' statement is leaving you with null where the type isn't&amp;nbsp;RebarShapeDefinitionBySegments. It may not be this but the first step I would take is to break down some of those 'if' statements into more lines. So you can step through more deeply to the issue rather than not having much of an idea because the breakpoint encompasses a single line with many statements.&lt;/P&gt;</description>
      <pubDate>Mon, 07 Mar 2022 18:08:08 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/system-nullreferenceexception-switching-python-code-to-c/m-p/10989243#M20401</guid>
      <dc:creator>RPTHOMAS108</dc:creator>
      <dc:date>2022-03-07T18:08:08Z</dc:date>
    </item>
    <item>
      <title>Re: System.NullReferenceException, Switching python code to c# language</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/system-nullreferenceexception-switching-python-code-to-c/m-p/10989280#M20402</link>
      <description>&lt;P&gt;Your problem is in&amp;nbsp;&lt;EM&gt;((doc.GetElement((reb as Rebar).GetShapeId()) as RebarShape)&lt;/EM&gt;. GetShapeId() returns an ElementId, not an element. You'll need to use&amp;nbsp;&lt;EM&gt;Document.GetElement(id)&lt;/EM&gt; to get the actual element.&lt;/P&gt;</description>
      <pubDate>Mon, 07 Mar 2022 18:30:45 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/system-nullreferenceexception-switching-python-code-to-c/m-p/10989280#M20402</guid>
      <dc:creator>mhannonQ65N2</dc:creator>
      <dc:date>2022-03-07T18:30:45Z</dc:date>
    </item>
    <item>
      <title>Re: System.NullReferenceException, Switching python code to c# language</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/system-nullreferenceexception-switching-python-code-to-c/m-p/10989339#M20403</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;Element element=doc.GetElement(rebar.GetShapeId()); 
int segments;
if (element != null)
{
    RebarShape	shape=element as RebarShape;
    RebarShapeDefinition definition=shape.GetRebarShapeDefinition();							 
    RebarShapeDefinitionBySegments definitionSeg= definition as RebarShapeDefinitionBySegments;						 
    segments=definitionSeg.NumberOfSegments;
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;thanks for replying me Thomas, I've broken it down into those lines and the null exception still triggering from that part of the code. Would you say that's exactly how you usually get the number of segments from a rebar element?&lt;/P&gt;</description>
      <pubDate>Mon, 07 Mar 2022 18:58:53 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/system-nullreferenceexception-switching-python-code-to-c/m-p/10989339#M20403</guid>
      <dc:creator>MiguelGT17</dc:creator>
      <dc:date>2022-03-07T18:58:53Z</dc:date>
    </item>
    <item>
      <title>Re: System.NullReferenceException, Switching python code to c# language</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/system-nullreferenceexception-switching-python-code-to-c/m-p/10989348#M20404</link>
      <description>&lt;P&gt;I'm quite sure this is how I get the element. I've working this way in python.&lt;/P&gt;</description>
      <pubDate>Mon, 07 Mar 2022 19:01:25 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/system-nullreferenceexception-switching-python-code-to-c/m-p/10989348#M20404</guid>
      <dc:creator>MiguelGT17</dc:creator>
      <dc:date>2022-03-07T19:01:25Z</dc:date>
    </item>
    <item>
      <title>Re: System.NullReferenceException, Switching python code to c# language</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/system-nullreferenceexception-switching-python-code-to-c/m-p/10989352#M20405</link>
      <description>&lt;P&gt;I don't know how python does things but &lt;A href="https://apidocs.co/apps/revit/2020/6edc946f-d8a3-ee78-adbb-7d5359501ed3.htm" target="_blank" rel="noopener"&gt;that method&lt;/A&gt; returns an ElementId in C#.&lt;/P&gt;</description>
      <pubDate>Mon, 07 Mar 2022 19:03:16 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/system-nullreferenceexception-switching-python-code-to-c/m-p/10989352#M20405</guid>
      <dc:creator>mhannonQ65N2</dc:creator>
      <dc:date>2022-03-07T19:03:16Z</dc:date>
    </item>
    <item>
      <title>Re: System.NullReferenceException, Switching python code to c# language</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/system-nullreferenceexception-switching-python-code-to-c/m-p/10989357#M20406</link>
      <description>&lt;P&gt;Thanks Thomas for pointing that out, Indeed I needed to make sure the&amp;nbsp;RebarShapeDefinitionBySegments variable was not null. It's a shame to not have the same power engine as Python to put all together at once &lt;span class="lia-unicode-emoji" title=":confused_face:"&gt;😕&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="general"&gt;Element element=doc.GetElement(rebar.GetShapeId()); 
RebarShape	shape=element as RebarShape;
RebarShapeDefinition definition=shape.GetRebarShapeDefinition();
RebarShapeDefinitionBySegments definitionSeg= definition as RebarShapeDefinitionBySegments;
if (definitionSeg != null)
{
    int segments = definitionSeg.NumberOfSegments;
    if ( segments &amp;gt; 1 )
    {
        //code
    }
    if (segments==1)
    {
        //code
    }&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 07 Mar 2022 20:07:14 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/system-nullreferenceexception-switching-python-code-to-c/m-p/10989357#M20406</guid>
      <dc:creator>MiguelGT17</dc:creator>
      <dc:date>2022-03-07T20:07:14Z</dc:date>
    </item>
    <item>
      <title>Re: System.NullReferenceException, Switching python code to c# language</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/system-nullreferenceexception-switching-python-code-to-c/m-p/10989361#M20407</link>
      <description>&lt;P&gt;Your are right, and this &lt;A href="https://www.revitapidocs.com/2021.1/d9848d7d-5917-2433-8454-f65f5ac03964.htm" target="_blank" rel="noopener"&gt;method&lt;/A&gt; reads an ElementId as well to grab the appropriate element&lt;/P&gt;</description>
      <pubDate>Mon, 07 Mar 2022 20:05:49 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/system-nullreferenceexception-switching-python-code-to-c/m-p/10989361#M20407</guid>
      <dc:creator>MiguelGT17</dc:creator>
      <dc:date>2022-03-07T20:05:49Z</dc:date>
    </item>
    <item>
      <title>Re: System.NullReferenceException, Switching python code to c# language</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/system-nullreferenceexception-switching-python-code-to-c/m-p/10989505#M20408</link>
      <description>&lt;P&gt;Hi Thomas, appreciate the time you put into this topic. By the way, I could not even use the step by step debug, Do I need to change the shardevelop settings to see that command?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="MiguelGT17_0-1646684120083.png" style="width: 600px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/1033398i06D1F0F0546E6088/image-size/medium?v=v2&amp;amp;px=400" role="button" title="MiguelGT17_0-1646684120083.png" alt="MiguelGT17_0-1646684120083.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 07 Mar 2022 20:15:44 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/system-nullreferenceexception-switching-python-code-to-c/m-p/10989505#M20408</guid>
      <dc:creator>MiguelGT17</dc:creator>
      <dc:date>2022-03-07T20:15:44Z</dc:date>
    </item>
    <item>
      <title>Re: System.NullReferenceException, Switching python code to c# language</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/system-nullreferenceexception-switching-python-code-to-c/m-p/10991259#M20409</link>
      <description>After placing your breakpoint, make sure Revit is running and select Attach to Process, then select Revit. After you've attached to Revit you can run your command and start debugging. Although, you'll need to have the latest build of your code in your addins folder in order for it to work correctly.</description>
      <pubDate>Tue, 08 Mar 2022 14:25:14 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/system-nullreferenceexception-switching-python-code-to-c/m-p/10991259#M20409</guid>
      <dc:creator>AGGilliam</dc:creator>
      <dc:date>2022-03-08T14:25:14Z</dc:date>
    </item>
    <item>
      <title>Re: System.NullReferenceException, Switching python code to c# language</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/system-nullreferenceexception-switching-python-code-to-c/m-p/10992645#M20410</link>
      <description>&lt;P&gt;When you press the 'Edit' button the below controls are not visible, so use 'Edit', place your breakpoint, keep the editor open in the background, go back to the Macro Manager window, then press 'Step Into'.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="220308a.png" style="width: 578px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/1034024iC8E0B5140E9D6FAF/image-size/large?v=v2&amp;amp;px=999" role="button" title="220308a.png" alt="220308a.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="220308.png" style="width: 950px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/1034025iD59D0C45FC083F8F/image-size/large?v=v2&amp;amp;px=999" role="button" title="220308.png" alt="220308.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 08 Mar 2022 23:51:31 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/system-nullreferenceexception-switching-python-code-to-c/m-p/10992645#M20410</guid>
      <dc:creator>RPTHOMAS108</dc:creator>
      <dc:date>2022-03-08T23:51:31Z</dc:date>
    </item>
    <item>
      <title>Re: System.NullReferenceException, Switching python code to c# language</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/system-nullreferenceexception-switching-python-code-to-c/m-p/10993937#M20411</link>
      <description>&lt;P&gt;My apologies, just realized you mentioned the macro manager. My previous comment was for addins. I never use macros so I kind of forgot they existed.&lt;/P&gt;</description>
      <pubDate>Wed, 09 Mar 2022 14:23:34 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/system-nullreferenceexception-switching-python-code-to-c/m-p/10993937#M20411</guid>
      <dc:creator>AGGilliam</dc:creator>
      <dc:date>2022-03-09T14:23:34Z</dc:date>
    </item>
    <item>
      <title>Re: System.NullReferenceException, Switching python code to c# language</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/system-nullreferenceexception-switching-python-code-to-c/m-p/10994860#M20412</link>
      <description>&lt;P&gt;Many thanks to both of you! I'm set to go with that one&lt;/P&gt;</description>
      <pubDate>Wed, 09 Mar 2022 21:33:21 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/system-nullreferenceexception-switching-python-code-to-c/m-p/10994860#M20412</guid>
      <dc:creator>MiguelGT17</dc:creator>
      <dc:date>2022-03-09T21:33:21Z</dc:date>
    </item>
  </channel>
</rss>

