.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

What this mean and how to fixed it: Cannot convert null to 'Autodesk.AutoCAD.Geo

4 REPLIES 4
Reply
Message 1 of 5
wesbird
361 Views, 4 Replies

What this mean and how to fixed it: Cannot convert null to 'Autodesk.AutoCAD.Geo

Hi,
I tried to create a function to calculate the center of a pline. first I like to check if pline is null. if yes, I will return null.
Now I got a error message:
"Cannot convert null to 'Autodesk.AutoCAD.Geometry.Point3d' because it is a value type"
How Point3d is value type?

here is the code.
private Point3d PlineCen(Entity _pline)
{
// todo
if (_pline == null) return null;
...


Thank you
Wes
Windows 10 64 bit, AutoCAD (ACA, Map) 2023
4 REPLIES 4
Message 2 of 5
Anonymous
in reply to: wesbird

Throw an ArgumentNullException instead; nullable types are coming in .NET
2.0.

If you really must have a "null" Point3d (throwing the exception is better),
initialize a read-only variable to something that makes sense for you such
as Double.MaxValue or Double.MinValue or maybe Double.NaN.

Dan

wrote in message news:4981339@discussion.autodesk.com...
Hi,
I tried to create a function to calculate the center of a pline. first I
like to check if pline is null. if yes, I will return null.
Now I got a error message:
"Cannot convert null to 'Autodesk.AutoCAD.Geometry.Point3d' because it is a
value type"
How Point3d is value type?

here is the code.
private Point3d PlineCen(Entity _pline)
{
// todo
if (_pline == null) return null;
...


Thank you
Wes
Message 3 of 5
Anonymous
in reply to: wesbird

In the case of an invalid argument, as Dan says, you really
should throw an exception, because calling the function without
a valid Entity parameter is almost certainly a hard error.

When you have cases like that, you are supposed to throw an
exception. If you don't, and instead return null or something
equally meaningless, it only results in hiding bugs and causing
them to manifest farther from the source.

For cases where they may not be a need to return a valid
Point3D (e.g., the polyline is a straight segment with no
center point), You can use a ref paramter instead:

private bool PlineCen(Entity _pline, ref Point3d center)
{
if( ! condition ) // condition = false if _pline has no center point
return false;
else
{
center = ComputeCenter(_pline); compute center point
return true;
}
}

You would call it like so:

Entity ent = //...
Point3d center;
if( PlineCen( ent, ref center ) )
// center point was found
else
// no center point found


--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2004/2005/2006
http://www.acadxtabs.com

wrote in message news:4981339@discussion.autodesk.com...
Hi,
I tried to create a function to calculate the center of a pline. first I like to check if pline is null. if yes, I will return null.
Now I got a error message:
"Cannot convert null to 'Autodesk.AutoCAD.Geometry.Point3d' because it is a value type"
How Point3d is value type?

here is the code.
private Point3d PlineCen(Entity _pline)
{
// todo
if (_pline == null) return null;
...


Thank you
Wes
Message 4 of 5
Anonymous
in reply to: wesbird

This starts to get into a (big?) gray area. Should you throw an
ArgumentException (or ArgumentOutOfRangeException) or return a status code?
I don't think there is a single right answer, it depends on the exact
semantics of PlineCen() and whether or not this kind of failure is
"expected" for normal operations (whatever *that* means 🙂 ). Here
(http://www.cuj.com/documents/s=9481/cuj0408g/0408mill.html) is an article
that may help: "When, for what, and how should you use exceptions instead of
error codes to report errors?" In this column, I propose a clear and
practical answer..."

An "out" (instead of "ref") parameter might be slightly better, although
then the compiler will force you to set "center" to some value. But at
least in this case, you can use an "I don't care" value--Point3d(0.0, 0.0,
0.0)--since you "know" it will never be used (everybody will check the
return value, right?).

Finally I forget to mention this...I know the rules for C# are different
than C++, but I'd "just not go there" and use Standard C++ names for
identifers. Avoid "_pline", "pline_" if you must; but why not just "pline"
in this case?

Dan

"Tony Tanzillo" wrote in message
news:4981703@discussion.autodesk.com...
In the case of an invalid argument, as Dan says, you really
should throw an exception, because calling the function without
a valid Entity parameter is almost certainly a hard error.

When you have cases like that, you are supposed to throw an
exception. If you don't, and instead return null or something
equally meaningless, it only results in hiding bugs and causing
them to manifest farther from the source.

For cases where they may not be a need to return a valid
Point3D (e.g., the polyline is a straight segment with no
center point), You can use a ref paramter instead:

private bool PlineCen(Entity _pline, ref Point3d center)
{
if( ! condition ) // condition = false if _pline has no center point
return false;
else
{
center = ComputeCenter(_pline); compute center point
return true;
}
}

You would call it like so:

Entity ent = //...
Point3d center;
if( PlineCen( ent, ref center ) )
// center point was found
else
// no center point found


--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2004/2005/2006
http://www.acadxtabs.com

wrote in message news:4981339@discussion.autodesk.com...
Hi,
I tried to create a function to calculate the center of a pline. first I
like to check if pline is null. if yes, I will return null.
Now I got a error message:
"Cannot convert null to 'Autodesk.AutoCAD.Geometry.Point3d' because it is a
value type"
How Point3d is value type?

here is the code.
private Point3d PlineCen(Entity _pline)
{
// todo
if (_pline == null) return null;
...


Thank you
Wes
Message 5 of 5
Anonymous
in reply to: wesbird

"J. Daniel Smith" wrote:

>> This starts to get into a (big?) gray area.
>> Should you throw an ArgumentException
>> (or ArgumentOutOfRangeException) or return
>> a status code?

I would throw the exception. A programmer does
not have to check a result, and if they don't and
the code is allowed to continue running, then it
can result in corruption of data.

If an exception is thrown, and the programmmer
calling the function does not handle it, then the
program will stop, which is what should happen.

That view is based on the notion that the worse
kind of bugs are not the ones that cause code to
fail, but rather, allow it to continue executing, in
a corrupt state.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2004/2005/2006
http://www.acadxtabs.com

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost