Creating Angular Dimensions with NXOpen

I have I problem while creating Dimensions to display in my NX model. When I execute my code I get an Error saying: The object referencing was not set to an object instance (something like that, because it's translated from German). Someone an idea? Code below


NXOpen.Line line1 = workPart.Curves.CreateLine(StartPoint, EndPoint);
theSession.ActiveSketch.AddGeometry(line1, NXOpen.Sketch.InferConstraintsOption.InferNoConstraints);

NXOpen.Point3d origin1 = new NXOpen.Point3d(0, 0, 0);
Point3d point2 = new Point3d(1, 0, 0);
NXOpen.Expression nullNXOpen_Expression = null;

NXOpen.Sketch.DimensionGeometry dimObject1_2 = new NXOpen.Sketch.DimensionGeometry();
dimObject1_2.Geometry = line1;
dimObject1_2.AssocType = NXOpen.Sketch.AssocType.EndPoint;
dimObject1_2.HelpPoint.X = 0;
dimObject1_2.HelpPoint.Y = 0;
dimObject1_2.HelpPoint.Z = 0;

NXOpen.Sketch.DimensionGeometry dimObject2_2 = new NXOpen.Sketch.DimensionGeometry();
NXOpen.DatumAxis ReferenceAxis = workPart.Datums.CreateFixedDatumAxis(origin1, point2);
dimObject2_2.Geometry = ReferenceAxis;
dimObject2_2.AssocType = NXOpen.Sketch.AssocType.EndPoint;
dimObject2_2.HelpPoint.X = 0;
dimObject2_2.HelpPoint.Y = 0;
dimObject2_2.HelpPoint.Z = 0;

NXOpen.Point3d dimOrigin2 = new NXOpen.Point3d(0, 0, 0);
NXOpen.SketchDimensionalConstraint sketchDimensionalConstraint2;

sketchDimensionalConstraint2 = theSession.ActiveSketch.CreateDimension(NXOpen.Sketch.ConstraintType.AngularDim, dimObject1_2, dimObject2_2, dimOrigin2, nullNXOpen_Expression, NXOpen.Sketch.DimensionOption.CreateAsAutomatic);

The call to .CreateFixedDatumAxis will create a datum axis feature. I assume that your code has created a sketch at this point (or is editing an existing sketch); if this is the case, the datum axis will have a later timestamp than the sketch. The sketch will not be able to reference a feature with a later timestamp. Try creating the datum axis before the sketch.

Thank you for your quick response! I tried your suggestion and declared my datum axis before I commited my sketch. But this time I get another Error saying "NXOpen.NXException: The input parameter is NULL or invalid (NXOpen.Sketch.AddGeometry(DisplayableObject crv)). All data-types are okay.

Without code, I'd just be guessing...

Yeah that's corrrect. Thank you for your help!
I solved my problem just one min ago using another way of creating dimensions (I used the sketchAngularDimensionBuilder instead)