Home All Groups Group Topic Archive Search About

null reference exception could result at runtime

Author
7 Apr 2010 6:50 PM
mp
vbnet 2008 express
how to avoid the compiler warning:
Variable 'ArrayObjects' is used before it has been assigned a value. A null
null
reference exception could result at runtime.

given
Dim ArrayObjects() As AcadEntity

....some code...

Try

ReDim ArrayObjects(0)

ArrayObjects(UBound(ArrayObjects)) = oSideLine'(oSideLine is an acad entity)

Catch ex As ApplicationException

m_Util.Logentry("error " & ex.Message)

End Try

....more code...then...

Try

If ArrayObjects Is Nothing Then <------------here is the warning location at
"ArrayObjects"

m_Util.Logentry("error ArrayObjects Is Nothing")

Else

SelectionSetWblock.AddItems(ArrayObjects)

End If

Catch ex As ApplicationException

End Try



what should I be doing in such cases (using an array)

i wouldn't think you'd do Dim ArrayObjects() As AcadEntity = Nothing

like you might with a simple object???

thanks

mark

Author
7 Apr 2010 7:32 PM
Armin Zingler
Am 07.04.2010 20:50, schrieb mp:
Show quoteHide quote
> vbnet 2008 express
> how to avoid the compiler warning:
> Variable 'ArrayObjects' is used before it has been assigned a value. A null
> null
> reference exception could result at runtime.
>
> given
> Dim ArrayObjects() As AcadEntity
>
> ....some code...
>
> Try
>
> ReDim ArrayObjects(0)
>
> ArrayObjects(UBound(ArrayObjects)) = oSideLine'(oSideLine is an acad entity)
>
> Catch ex As ApplicationException
>
> m_Util.Logentry("error " & ex.Message)
>
> End Try
>
> ....more code...then...
>
> Try
>
> If ArrayObjects Is Nothing Then <------------here is the warning location at
> "ArrayObjects"
>
> m_Util.Logentry("error ArrayObjects Is Nothing")
>
> Else
>
> SelectionSetWblock.AddItems(ArrayObjects)
>
> End If
>
> Catch ex As ApplicationException
>
> End Try
>
>
>
> what should I be doing in such cases (using an array)
>
> i wouldn't think you'd do Dim ArrayObjects() As AcadEntity = Nothing
>
> like you might with a simple object???

Well, what is a not-simple object? An array is an object, so you can assign
Nothing in the declaration line as you've checked that the warning isn't
justified in this case.

--
Armin
Author
7 Apr 2010 8:52 PM
Mr. Arnold
mp wrote:
>
> If ArrayObjects Is Nothing Then <------------here is the warning location at
> "ArrayObjects"
>

You only want to do something, work with the object, if it is 'NOT' null.

So you have an if statement checking for the NOT null condition.

On the other hand, if the object is never going to be null, then why
worry about? It's just a warning message.
Author
7 Apr 2010 9:04 PM
mp
Show quote Hide quote
"Mr. Arnold" <Arn***@Arnold.com> wrote in message
news:Ok%23zQRp1KHA.4724@TK2MSFTNGP02.phx.gbl...
> mp wrote:
>>
>> If ArrayObjects Is Nothing Then <------------here is the warning location
>> at
>> "ArrayObjects"
>>
>
> You only want to do something, work with the object, if it is 'NOT' null.
>
> So you have an if statement checking for the NOT null condition.
>
> On the other hand, if the object is never going to be null, then why worry
> about? It's just a warning message.

ok, i just thought the warning was telling me i was doing something "wrong"
and trying to learn best practices
thanks Mr Arnold and Armin
Author
8 Apr 2010 12:30 PM
Phill W.
On 07/04/2010 19:50, mp wrote:

> how to avoid the compiler warning:
> Variable 'ArrayObjects' is used before it has been assigned a value. A null
> null reference exception could result at runtime.

> Dim ArrayObjects() As AcadEntity

The compiler is warning you that this variable /might/ have no value
when you come to use it.

Avoid this by giving the variable a value - even if that value just
happens to be a /null/ one!

    Dim ArrayObjects As AcadEntity() = Nothing

(I prefer the array braces with the Type, not the Variable - it just
reads better, IMHO - has the same effect either way).

OK, you /will/ still get a NullReferenceException later on if you don't
give it another value, but it'll be your all own doing.  :-)

HTH,
    Phill  W.
Author
8 Apr 2010 3:39 PM
mp
Thanks, I'd seen declarations setting to nothing with "regular objects", but
was thinking an array was an array, not an object...
It was cleared up for me that arrays are objects in dot net...i'm slowly
getting that through my thick skull that everyting is an object in dot net.
Thanks for your response
Mark


Show quoteHide quote
"Phill W." <p-.-a-.-w-a-r-d-@-o-p-e-n-.-a-c-.-u-k> wrote in message
news:hpki8s$4vm$1@south.jnrs.ja.net...
> On 07/04/2010 19:50, mp wrote:
>
>> how to avoid the compiler warning:
>> Variable 'ArrayObjects' is used before it has been assigned a value. A
>> null
>> null reference exception could result at runtime.
>
>> Dim ArrayObjects() As AcadEntity
>
> The compiler is warning you that this variable /might/ have no value when
> you come to use it.
>
> Avoid this by giving the variable a value - even if that value just
> happens to be a /null/ one!
>
>    Dim ArrayObjects As AcadEntity() = Nothing
>
> (I prefer the array braces with the Type, not the Variable - it just reads
> better, IMHO - has the same effect either way).
>
> OK, you /will/ still get a NullReferenceException later on if you don't
> give it another value, but it'll be your all own doing.  :-)
>
> HTH,
>    Phill  W.