.NET

Reply
Distinguished Contributor
Posts: 172
Registered: ‎11-19-2003
Message 1 of 2 (157 Views)

Error System.NullReferenceException in code - HELP!!!

157 Views, 1 Replies
06-08-2005 06:30 AM
What is wrong with this code, i allways get this error:
"System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.NullReferenceException: Object reference not set to an instance of an object.
at RSNNAcadApp.ExtendedCommands.Distance.DistanceOnCurve()"

[code]#region Using directives

using System;
//using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Globalization;

using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;

using System.Runtime.InteropServices;

#endregion

namespace RSNNAcadApp.ExtendedCommands
{
public class Distance
{
//this is an undocumented api exported from acad.exe. Use it at your own risk.
//
// Setting this flag tells AutoCAD to display the last string output to the command line
//in the Dynamic Input prompt window (one time only.)
[DllImport("acad.exe", EntryPoint = "?acedSetDynInputDisplayMessage@@YA_N_N@Z")]
private static extern bool acedSetDynInputDisplayMessage(bool displayMessageOnce);

private int Luprec = Application.DocumentManager.MdiActiveDocument.Database.Luprec;

private Curve CurveObj;
private Point3d FirstPoint;
private Point3d SecondPoint;

private double DistFP;
private double DistSP;
private double Dist;
private string DistFPPrompt;
private string DistSPPrompt;
private string DistPrompt;

public Distance()
{
}

[CommandMethod("AbstandKurve", CommandFlags.Modal)]
public void DistanceOnCurve()
{

Database db = HostApplicationServices.WorkingDatabase;
Transaction myT = db.TransactionManager.StartTransaction();
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
try
{
PromptEntityOptions entopts = new PromptEntityOptions("Objekt zeigen");
entopts.Message = "Objekt zeigen";
PromptEntityResult ent = null;
bool RightObject = false;

while (!RightObject)
{
while (ent.Status == PromptStatus.Error)
{
ent = ed.GetEntity(entopts);
}

if (ent.Status == PromptStatus.Cancel)
return;

if (ent.Status == PromptStatus.OK)
{
ObjectId entid1 = ent.ObjectId;
Entity tmpEnt = (Entity)myT.GetObject(entid1, OpenMode.ForRead);
CurveObj = tmpEnt as Curve;
if (CurveObj != null)
{
RightObject = true;

//highlight the entity
ObjectId[] ids = new ObjectId[1];
ids[0] = entid1;

SubentityId index = new SubentityId(SubentityType.Edge, 0);
FullSubentityPath path = new FullSubentityPath(ids, index);

tmpEnt.Highlight(path, true);

bool PromptCancel = false;


PromptPointOptions FPOption = new PromptPointOptions("Ersten Punkt angeben");
FPOption.Message = "Anfangspunkt zeigen";
PromptPointResult FPResult = null;

while (FPResult.Status == PromptStatus.Error)
{
FPResult = ed.GetPoint(FPOption);
FPOption.Message = "\nFalsche Eingabe. Richtung zeigen";
}

if (FPResult.Status == PromptStatus.Cancel)
PromptCancel = true;

if (FPResult.Status == PromptStatus.OK)
{
Point3d tmpStartPoint = FPResult.Value;
FirstPoint = CurveObj.GetClosestPointTo(tmpStartPoint, false);
DistFP = CurveObj.GetDistAtPoint(FirstPoint);
}

if (!PromptCancel)
{
PromptPointOptions SPOption = new PromptPointOptions("Zweiten Punkt angeben");
SPOption.BasePoint = FirstPoint;
SPOption.UseBasePoint = true;
SPOption.UseDashedLine = true;
SPOption.Message = "Richtung zeigen";
PromptPointResult SPResult = null;

while (SPResult.Status == PromptStatus.Error)
{
SPResult = ed.GetPoint(SPOption);
SPOption.Message = "\nFalsche Eingabe. Richtung zeigen";
}

if (SPResult.Status == PromptStatus.Cancel)
return;

if (SPResult.Status == PromptStatus.OK &&
FPResult.Status == PromptStatus.OK)
{
Point3d tmpDirectionPoint = SPResult.Value;
SecondPoint = CurveObj.GetClosestPointTo(tmpDirectionPoint, false);
DistSP = CurveObj.GetDistAtPoint(SecondPoint);
}

}

tmpEnt.Unhighlight(path, false);

}
}
}
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-us");
DistFPPrompt = DistFP.ToString("F" + Luprec.ToString());
DistSPPrompt = DistSP.ToString("F" + Luprec.ToString());
Dist = Math.Abs(DistSP - DistFP);
DistPrompt = Dist.ToString("F" + Luprec.ToString());

ed.WriteMessage(String.Format("\rAbstand = {0}, erster Punkt = {1}, zweiter Punkt = {2}",
DistPrompt, DistFPPrompt, DistSPPrompt));
acedSetDynInputDisplayMessage(true);

myT.Commit();
}
catch (System.Exception)
{
myT.Commit();
throw;
}

finally
{
myT.Dispose();
}

}

}
}[/code]
Distinguished Contributor
Posts: 172
Registered: ‎11-19-2003
Message 2 of 2 (157 Views)

Re: Error System.NullReferenceException in code - HELP!!!

06-08-2005 07:39 AM in reply to: RolandF
I've got it, it was just a silly mistake.

Changed:
[code] while (ent.Status == PromptStatus.Error)
{
ent = ed.GetEntity(entopts);
}[/code]
To:
[code] do
{
ent = ed.GetEntity(entopts);
} while (ent.Status == PromptStatus.Error);[/code]

and so on.

Roland

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Are you interested in helping shape the Autodesk Community?
We’re looking at a few different ways to improve the “All Forums” landing page and need your feedback! If interested, please take a few minutes to fill out the following Usability Study. Thank you for your time!

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community