null reference exception could result at runtime

  • Thread starter Thread starter mp
  • Start date Start date
M

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
 
Am 07.04.2010 20:50, schrieb 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???

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.
 
mp said:
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.
 
Mr. Arnold said:
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
 
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.
 
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
 
Back
Top