<?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 Error System.NullReferenceException in code - HELP!!! in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/error-system-nullreferenceexception-in-code-help/m-p/1348072#M84721</link>
    <description>What is wrong with this code, i allways get this error:&lt;BR /&gt;
"System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---&amp;gt; System.NullReferenceException: Object reference not set to an instance of an object.&lt;BR /&gt;
   at RSNNAcadApp.ExtendedCommands.Distance.DistanceOnCurve()"&lt;BR /&gt;
&lt;BR /&gt;
[code]#region Using directives&lt;BR /&gt;
&lt;BR /&gt;
using System;&lt;BR /&gt;
//using System.Collections.Generic;&lt;BR /&gt;
using System.Text;&lt;BR /&gt;
using System.Threading;&lt;BR /&gt;
using System.Globalization;&lt;BR /&gt;
&lt;BR /&gt;
using Autodesk.AutoCAD.Runtime;&lt;BR /&gt;
using Autodesk.AutoCAD.ApplicationServices;&lt;BR /&gt;
using Autodesk.AutoCAD.Geometry;&lt;BR /&gt;
using Autodesk.AutoCAD.DatabaseServices;&lt;BR /&gt;
using Autodesk.AutoCAD.EditorInput;&lt;BR /&gt;
&lt;BR /&gt;
using System.Runtime.InteropServices;&lt;BR /&gt;
&lt;BR /&gt;
#endregion&lt;BR /&gt;
&lt;BR /&gt;
namespace RSNNAcadApp.ExtendedCommands&lt;BR /&gt;
{&lt;BR /&gt;
    public class Distance&lt;BR /&gt;
    {&lt;BR /&gt;
        //this is an undocumented api exported from acad.exe. Use it at your own risk.&lt;BR /&gt;
        //&lt;BR /&gt;
        // Setting this flag tells AutoCAD to display the last string output to the command line&lt;BR /&gt;
        //in the Dynamic Input prompt window (one time only.)&lt;BR /&gt;
        [DllImport("acad.exe", EntryPoint = "?acedSetDynInputDisplayMessage@@YA_N_N@Z")]&lt;BR /&gt;
        private static extern bool acedSetDynInputDisplayMessage(bool displayMessageOnce);&lt;BR /&gt;
&lt;BR /&gt;
        private int Luprec = Application.DocumentManager.MdiActiveDocument.Database.Luprec;&lt;BR /&gt;
&lt;BR /&gt;
        private Curve CurveObj;&lt;BR /&gt;
        private Point3d FirstPoint;&lt;BR /&gt;
        private Point3d SecondPoint;&lt;BR /&gt;
&lt;BR /&gt;
        private double DistFP;&lt;BR /&gt;
        private double DistSP;&lt;BR /&gt;
        private double Dist;&lt;BR /&gt;
        private string DistFPPrompt;&lt;BR /&gt;
        private string DistSPPrompt;&lt;BR /&gt;
        private string DistPrompt;&lt;BR /&gt;
&lt;BR /&gt;
        public Distance()&lt;BR /&gt;
        {&lt;BR /&gt;
        }&lt;BR /&gt;
&lt;BR /&gt;
        [CommandMethod("AbstandKurve", CommandFlags.Modal)]&lt;BR /&gt;
        public void DistanceOnCurve()&lt;BR /&gt;
        {&lt;BR /&gt;
&lt;BR /&gt;
            Database db = HostApplicationServices.WorkingDatabase;&lt;BR /&gt;
            Transaction myT = db.TransactionManager.StartTransaction();&lt;BR /&gt;
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;&lt;BR /&gt;
            try&lt;BR /&gt;
            {&lt;BR /&gt;
                PromptEntityOptions entopts = new PromptEntityOptions("Objekt zeigen");&lt;BR /&gt;
                entopts.Message = "Objekt zeigen";&lt;BR /&gt;
                PromptEntityResult ent = null;&lt;BR /&gt;
                bool RightObject = false;&lt;BR /&gt;
&lt;BR /&gt;
                while (!RightObject)&lt;BR /&gt;
                {&lt;BR /&gt;
                    while (ent.Status == PromptStatus.Error)&lt;BR /&gt;
                    {&lt;BR /&gt;
                        ent = ed.GetEntity(entopts);&lt;BR /&gt;
                    }&lt;BR /&gt;
&lt;BR /&gt;
                    if (ent.Status == PromptStatus.Cancel)&lt;BR /&gt;
                        return;&lt;BR /&gt;
&lt;BR /&gt;
                    if (ent.Status == PromptStatus.OK)&lt;BR /&gt;
                    {&lt;BR /&gt;
                        ObjectId entid1 = ent.ObjectId;&lt;BR /&gt;
                        Entity tmpEnt = (Entity)myT.GetObject(entid1, OpenMode.ForRead);&lt;BR /&gt;
                        CurveObj = tmpEnt as Curve;&lt;BR /&gt;
                        if (CurveObj != null)&lt;BR /&gt;
                        {&lt;BR /&gt;
                            RightObject = true;&lt;BR /&gt;
&lt;BR /&gt;
                            //highlight the entity&lt;BR /&gt;
                            ObjectId[] ids = new ObjectId[1];&lt;BR /&gt;
                            ids[0] = entid1;&lt;BR /&gt;
&lt;BR /&gt;
                            SubentityId index = new SubentityId(SubentityType.Edge, 0);&lt;BR /&gt;
                            FullSubentityPath path = new FullSubentityPath(ids, index);&lt;BR /&gt;
&lt;BR /&gt;
                            tmpEnt.Highlight(path, true);&lt;BR /&gt;
&lt;BR /&gt;
                            bool PromptCancel = false;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
                            PromptPointOptions FPOption = new PromptPointOptions("Ersten Punkt angeben");&lt;BR /&gt;
                            FPOption.Message = "Anfangspunkt zeigen";&lt;BR /&gt;
                            PromptPointResult FPResult = null;&lt;BR /&gt;
&lt;BR /&gt;
                            while (FPResult.Status == PromptStatus.Error)&lt;BR /&gt;
                            {&lt;BR /&gt;
                                FPResult = ed.GetPoint(FPOption);&lt;BR /&gt;
                                FPOption.Message = "\nFalsche Eingabe. Richtung zeigen";&lt;BR /&gt;
                            }&lt;BR /&gt;
&lt;BR /&gt;
                            if (FPResult.Status == PromptStatus.Cancel)&lt;BR /&gt;
                                PromptCancel = true;&lt;BR /&gt;
&lt;BR /&gt;
                            if (FPResult.Status == PromptStatus.OK)&lt;BR /&gt;
                            {&lt;BR /&gt;
                                Point3d tmpStartPoint = FPResult.Value;&lt;BR /&gt;
                                FirstPoint = CurveObj.GetClosestPointTo(tmpStartPoint, false);&lt;BR /&gt;
                                DistFP = CurveObj.GetDistAtPoint(FirstPoint);&lt;BR /&gt;
                            }&lt;BR /&gt;
&lt;BR /&gt;
                            if (!PromptCancel)&lt;BR /&gt;
                            {&lt;BR /&gt;
                                PromptPointOptions SPOption = new PromptPointOptions("Zweiten Punkt angeben");&lt;BR /&gt;
                                SPOption.BasePoint = FirstPoint;&lt;BR /&gt;
                                SPOption.UseBasePoint = true;&lt;BR /&gt;
                                SPOption.UseDashedLine = true;&lt;BR /&gt;
                                SPOption.Message = "Richtung zeigen";&lt;BR /&gt;
                                PromptPointResult SPResult = null;&lt;BR /&gt;
&lt;BR /&gt;
                                while (SPResult.Status == PromptStatus.Error)&lt;BR /&gt;
                                {&lt;BR /&gt;
                                    SPResult = ed.GetPoint(SPOption);&lt;BR /&gt;
                                    SPOption.Message = "\nFalsche Eingabe. Richtung zeigen";&lt;BR /&gt;
                                }&lt;BR /&gt;
&lt;BR /&gt;
                                if (SPResult.Status == PromptStatus.Cancel)&lt;BR /&gt;
                                    return;&lt;BR /&gt;
&lt;BR /&gt;
                                if (SPResult.Status == PromptStatus.OK &amp;amp;&amp;amp;&lt;BR /&gt;
                                    FPResult.Status == PromptStatus.OK)&lt;BR /&gt;
                                {&lt;BR /&gt;
                                    Point3d tmpDirectionPoint = SPResult.Value;&lt;BR /&gt;
                                    SecondPoint = CurveObj.GetClosestPointTo(tmpDirectionPoint, false);&lt;BR /&gt;
                                    DistSP = CurveObj.GetDistAtPoint(SecondPoint);&lt;BR /&gt;
                                }&lt;BR /&gt;
&lt;BR /&gt;
                            }&lt;BR /&gt;
&lt;BR /&gt;
                            tmpEnt.Unhighlight(path, false);&lt;BR /&gt;
&lt;BR /&gt;
                        }&lt;BR /&gt;
                    }&lt;BR /&gt;
                }&lt;BR /&gt;
                Thread.CurrentThread.CurrentCulture = new CultureInfo("en-us");&lt;BR /&gt;
                DistFPPrompt = DistFP.ToString("F" + Luprec.ToString());&lt;BR /&gt;
                DistSPPrompt = DistSP.ToString("F" + Luprec.ToString());&lt;BR /&gt;
                Dist = Math.Abs(DistSP - DistFP);&lt;BR /&gt;
                DistPrompt = Dist.ToString("F" + Luprec.ToString());&lt;BR /&gt;
&lt;BR /&gt;
                ed.WriteMessage(String.Format("\rAbstand = {0}, erster Punkt = {1}, zweiter Punkt = {2}",&lt;BR /&gt;
                                                DistPrompt, DistFPPrompt, DistSPPrompt));&lt;BR /&gt;
                acedSetDynInputDisplayMessage(true);&lt;BR /&gt;
&lt;BR /&gt;
                myT.Commit();&lt;BR /&gt;
            }&lt;BR /&gt;
            catch (System.Exception)&lt;BR /&gt;
            {&lt;BR /&gt;
                myT.Commit();&lt;BR /&gt;
                throw;&lt;BR /&gt;
            }&lt;BR /&gt;
&lt;BR /&gt;
            finally&lt;BR /&gt;
            {&lt;BR /&gt;
                myT.Dispose();&lt;BR /&gt;
            }&lt;BR /&gt;
&lt;BR /&gt;
        }&lt;BR /&gt;
&lt;BR /&gt;
    }&lt;BR /&gt;
}[/code]</description>
    <pubDate>Wed, 08 Jun 2005 13:30:14 GMT</pubDate>
    <dc:creator>Anonymous</dc:creator>
    <dc:date>2005-06-08T13:30:14Z</dc:date>
    <item>
      <title>Error System.NullReferenceException in code - HELP!!!</title>
      <link>https://forums.autodesk.com/t5/net-forum/error-system-nullreferenceexception-in-code-help/m-p/1348072#M84721</link>
      <description>What is wrong with this code, i allways get this error:&lt;BR /&gt;
"System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---&amp;gt; System.NullReferenceException: Object reference not set to an instance of an object.&lt;BR /&gt;
   at RSNNAcadApp.ExtendedCommands.Distance.DistanceOnCurve()"&lt;BR /&gt;
&lt;BR /&gt;
[code]#region Using directives&lt;BR /&gt;
&lt;BR /&gt;
using System;&lt;BR /&gt;
//using System.Collections.Generic;&lt;BR /&gt;
using System.Text;&lt;BR /&gt;
using System.Threading;&lt;BR /&gt;
using System.Globalization;&lt;BR /&gt;
&lt;BR /&gt;
using Autodesk.AutoCAD.Runtime;&lt;BR /&gt;
using Autodesk.AutoCAD.ApplicationServices;&lt;BR /&gt;
using Autodesk.AutoCAD.Geometry;&lt;BR /&gt;
using Autodesk.AutoCAD.DatabaseServices;&lt;BR /&gt;
using Autodesk.AutoCAD.EditorInput;&lt;BR /&gt;
&lt;BR /&gt;
using System.Runtime.InteropServices;&lt;BR /&gt;
&lt;BR /&gt;
#endregion&lt;BR /&gt;
&lt;BR /&gt;
namespace RSNNAcadApp.ExtendedCommands&lt;BR /&gt;
{&lt;BR /&gt;
    public class Distance&lt;BR /&gt;
    {&lt;BR /&gt;
        //this is an undocumented api exported from acad.exe. Use it at your own risk.&lt;BR /&gt;
        //&lt;BR /&gt;
        // Setting this flag tells AutoCAD to display the last string output to the command line&lt;BR /&gt;
        //in the Dynamic Input prompt window (one time only.)&lt;BR /&gt;
        [DllImport("acad.exe", EntryPoint = "?acedSetDynInputDisplayMessage@@YA_N_N@Z")]&lt;BR /&gt;
        private static extern bool acedSetDynInputDisplayMessage(bool displayMessageOnce);&lt;BR /&gt;
&lt;BR /&gt;
        private int Luprec = Application.DocumentManager.MdiActiveDocument.Database.Luprec;&lt;BR /&gt;
&lt;BR /&gt;
        private Curve CurveObj;&lt;BR /&gt;
        private Point3d FirstPoint;&lt;BR /&gt;
        private Point3d SecondPoint;&lt;BR /&gt;
&lt;BR /&gt;
        private double DistFP;&lt;BR /&gt;
        private double DistSP;&lt;BR /&gt;
        private double Dist;&lt;BR /&gt;
        private string DistFPPrompt;&lt;BR /&gt;
        private string DistSPPrompt;&lt;BR /&gt;
        private string DistPrompt;&lt;BR /&gt;
&lt;BR /&gt;
        public Distance()&lt;BR /&gt;
        {&lt;BR /&gt;
        }&lt;BR /&gt;
&lt;BR /&gt;
        [CommandMethod("AbstandKurve", CommandFlags.Modal)]&lt;BR /&gt;
        public void DistanceOnCurve()&lt;BR /&gt;
        {&lt;BR /&gt;
&lt;BR /&gt;
            Database db = HostApplicationServices.WorkingDatabase;&lt;BR /&gt;
            Transaction myT = db.TransactionManager.StartTransaction();&lt;BR /&gt;
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;&lt;BR /&gt;
            try&lt;BR /&gt;
            {&lt;BR /&gt;
                PromptEntityOptions entopts = new PromptEntityOptions("Objekt zeigen");&lt;BR /&gt;
                entopts.Message = "Objekt zeigen";&lt;BR /&gt;
                PromptEntityResult ent = null;&lt;BR /&gt;
                bool RightObject = false;&lt;BR /&gt;
&lt;BR /&gt;
                while (!RightObject)&lt;BR /&gt;
                {&lt;BR /&gt;
                    while (ent.Status == PromptStatus.Error)&lt;BR /&gt;
                    {&lt;BR /&gt;
                        ent = ed.GetEntity(entopts);&lt;BR /&gt;
                    }&lt;BR /&gt;
&lt;BR /&gt;
                    if (ent.Status == PromptStatus.Cancel)&lt;BR /&gt;
                        return;&lt;BR /&gt;
&lt;BR /&gt;
                    if (ent.Status == PromptStatus.OK)&lt;BR /&gt;
                    {&lt;BR /&gt;
                        ObjectId entid1 = ent.ObjectId;&lt;BR /&gt;
                        Entity tmpEnt = (Entity)myT.GetObject(entid1, OpenMode.ForRead);&lt;BR /&gt;
                        CurveObj = tmpEnt as Curve;&lt;BR /&gt;
                        if (CurveObj != null)&lt;BR /&gt;
                        {&lt;BR /&gt;
                            RightObject = true;&lt;BR /&gt;
&lt;BR /&gt;
                            //highlight the entity&lt;BR /&gt;
                            ObjectId[] ids = new ObjectId[1];&lt;BR /&gt;
                            ids[0] = entid1;&lt;BR /&gt;
&lt;BR /&gt;
                            SubentityId index = new SubentityId(SubentityType.Edge, 0);&lt;BR /&gt;
                            FullSubentityPath path = new FullSubentityPath(ids, index);&lt;BR /&gt;
&lt;BR /&gt;
                            tmpEnt.Highlight(path, true);&lt;BR /&gt;
&lt;BR /&gt;
                            bool PromptCancel = false;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
                            PromptPointOptions FPOption = new PromptPointOptions("Ersten Punkt angeben");&lt;BR /&gt;
                            FPOption.Message = "Anfangspunkt zeigen";&lt;BR /&gt;
                            PromptPointResult FPResult = null;&lt;BR /&gt;
&lt;BR /&gt;
                            while (FPResult.Status == PromptStatus.Error)&lt;BR /&gt;
                            {&lt;BR /&gt;
                                FPResult = ed.GetPoint(FPOption);&lt;BR /&gt;
                                FPOption.Message = "\nFalsche Eingabe. Richtung zeigen";&lt;BR /&gt;
                            }&lt;BR /&gt;
&lt;BR /&gt;
                            if (FPResult.Status == PromptStatus.Cancel)&lt;BR /&gt;
                                PromptCancel = true;&lt;BR /&gt;
&lt;BR /&gt;
                            if (FPResult.Status == PromptStatus.OK)&lt;BR /&gt;
                            {&lt;BR /&gt;
                                Point3d tmpStartPoint = FPResult.Value;&lt;BR /&gt;
                                FirstPoint = CurveObj.GetClosestPointTo(tmpStartPoint, false);&lt;BR /&gt;
                                DistFP = CurveObj.GetDistAtPoint(FirstPoint);&lt;BR /&gt;
                            }&lt;BR /&gt;
&lt;BR /&gt;
                            if (!PromptCancel)&lt;BR /&gt;
                            {&lt;BR /&gt;
                                PromptPointOptions SPOption = new PromptPointOptions("Zweiten Punkt angeben");&lt;BR /&gt;
                                SPOption.BasePoint = FirstPoint;&lt;BR /&gt;
                                SPOption.UseBasePoint = true;&lt;BR /&gt;
                                SPOption.UseDashedLine = true;&lt;BR /&gt;
                                SPOption.Message = "Richtung zeigen";&lt;BR /&gt;
                                PromptPointResult SPResult = null;&lt;BR /&gt;
&lt;BR /&gt;
                                while (SPResult.Status == PromptStatus.Error)&lt;BR /&gt;
                                {&lt;BR /&gt;
                                    SPResult = ed.GetPoint(SPOption);&lt;BR /&gt;
                                    SPOption.Message = "\nFalsche Eingabe. Richtung zeigen";&lt;BR /&gt;
                                }&lt;BR /&gt;
&lt;BR /&gt;
                                if (SPResult.Status == PromptStatus.Cancel)&lt;BR /&gt;
                                    return;&lt;BR /&gt;
&lt;BR /&gt;
                                if (SPResult.Status == PromptStatus.OK &amp;amp;&amp;amp;&lt;BR /&gt;
                                    FPResult.Status == PromptStatus.OK)&lt;BR /&gt;
                                {&lt;BR /&gt;
                                    Point3d tmpDirectionPoint = SPResult.Value;&lt;BR /&gt;
                                    SecondPoint = CurveObj.GetClosestPointTo(tmpDirectionPoint, false);&lt;BR /&gt;
                                    DistSP = CurveObj.GetDistAtPoint(SecondPoint);&lt;BR /&gt;
                                }&lt;BR /&gt;
&lt;BR /&gt;
                            }&lt;BR /&gt;
&lt;BR /&gt;
                            tmpEnt.Unhighlight(path, false);&lt;BR /&gt;
&lt;BR /&gt;
                        }&lt;BR /&gt;
                    }&lt;BR /&gt;
                }&lt;BR /&gt;
                Thread.CurrentThread.CurrentCulture = new CultureInfo("en-us");&lt;BR /&gt;
                DistFPPrompt = DistFP.ToString("F" + Luprec.ToString());&lt;BR /&gt;
                DistSPPrompt = DistSP.ToString("F" + Luprec.ToString());&lt;BR /&gt;
                Dist = Math.Abs(DistSP - DistFP);&lt;BR /&gt;
                DistPrompt = Dist.ToString("F" + Luprec.ToString());&lt;BR /&gt;
&lt;BR /&gt;
                ed.WriteMessage(String.Format("\rAbstand = {0}, erster Punkt = {1}, zweiter Punkt = {2}",&lt;BR /&gt;
                                                DistPrompt, DistFPPrompt, DistSPPrompt));&lt;BR /&gt;
                acedSetDynInputDisplayMessage(true);&lt;BR /&gt;
&lt;BR /&gt;
                myT.Commit();&lt;BR /&gt;
            }&lt;BR /&gt;
            catch (System.Exception)&lt;BR /&gt;
            {&lt;BR /&gt;
                myT.Commit();&lt;BR /&gt;
                throw;&lt;BR /&gt;
            }&lt;BR /&gt;
&lt;BR /&gt;
            finally&lt;BR /&gt;
            {&lt;BR /&gt;
                myT.Dispose();&lt;BR /&gt;
            }&lt;BR /&gt;
&lt;BR /&gt;
        }&lt;BR /&gt;
&lt;BR /&gt;
    }&lt;BR /&gt;
}[/code]</description>
      <pubDate>Wed, 08 Jun 2005 13:30:14 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/error-system-nullreferenceexception-in-code-help/m-p/1348072#M84721</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2005-06-08T13:30:14Z</dc:date>
    </item>
    <item>
      <title>Re: Error System.NullReferenceException in code - HELP!!!</title>
      <link>https://forums.autodesk.com/t5/net-forum/error-system-nullreferenceexception-in-code-help/m-p/1348073#M84722</link>
      <description>I've got it, it was just a silly mistake.&lt;BR /&gt;
&lt;BR /&gt;
Changed:&lt;BR /&gt;
[code]                    while (ent.Status == PromptStatus.Error)&lt;BR /&gt;
                    {&lt;BR /&gt;
                        ent = ed.GetEntity(entopts);&lt;BR /&gt;
                    }[/code]&lt;BR /&gt;
To:&lt;BR /&gt;
[code]                    do&lt;BR /&gt;
                    {&lt;BR /&gt;
                        ent = ed.GetEntity(entopts);&lt;BR /&gt;
                    } while (ent.Status == PromptStatus.Error);[/code]&lt;BR /&gt;
&lt;BR /&gt;
and so on.&lt;BR /&gt;
&lt;BR /&gt;
Roland</description>
      <pubDate>Wed, 08 Jun 2005 14:39:05 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/error-system-nullreferenceexception-in-code-help/m-p/1348073#M84722</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2005-06-08T14:39:05Z</dc:date>
    </item>
  </channel>
</rss>

