pMarker not assigned a value?

439
2
Jump to solution
04-09-2013 05:55 AM
BrianBaldwin1
New Contributor III
Quick question.  I have some old code that I have brought over from VB Script and just wanted to see why this error is coming up.

Warning: Variable 'pMarker' is used before it has been assigned a value. A null reference exception could result at runtime.

From what I can see, I'm assigning a value in the Do Until Loop...is there something I'm missing?

Dim pMarker As IMarkerSymbol         Do Until pStyleItem Is Nothing             If pStyleItem.Name = "Star 1" Then                 pMarker = pStyleItem.Item                 pMarker.Size = 12                 pMarker.Color = pColor                 Exit Do             End If             pStyleItem = pEnumMarkers.Next         Loop          Dim prenderer As ISimpleRenderer         prenderer = New SimpleRenderer         prenderer.Symbol = pMarker
0 Kudos
1 Solution

Accepted Solutions
NeilClemmons
Regular Contributor III
You are declaring the variable outside of the loop.  While it is true that you are assigning it a value inside the loop, it is possible that the loop could be skipped and never iterate (if pStyleItem were Nothing from the start) which would leave the variable unassigned when you use it to assign a value to prenderer.Symbol.  You can always avoid this problem by explicitly assigning the variable a value when you declare it:

Dim pMarker As IMarkerSymbol = Nothing

The line above doesn't actually change anything as pMarker is initialized to Nothing by default and will not be given an actual value in the case where the loop doesn't iterate, but it will make the warning go away.  You could also go into Settings and turn the warning off but I think that's a bad idea because getting the warning makes you review your code, which in turn could help you find and fix a logic error that you may have otherwise missed.

View solution in original post

0 Kudos
2 Replies
NeilClemmons
Regular Contributor III
You are declaring the variable outside of the loop.  While it is true that you are assigning it a value inside the loop, it is possible that the loop could be skipped and never iterate (if pStyleItem were Nothing from the start) which would leave the variable unassigned when you use it to assign a value to prenderer.Symbol.  You can always avoid this problem by explicitly assigning the variable a value when you declare it:

Dim pMarker As IMarkerSymbol = Nothing

The line above doesn't actually change anything as pMarker is initialized to Nothing by default and will not be given an actual value in the case where the loop doesn't iterate, but it will make the warning go away.  You could also go into Settings and turn the warning off but I think that's a bad idea because getting the warning makes you review your code, which in turn could help you find and fix a logic error that you may have otherwise missed.
0 Kudos
BrianBaldwin1
New Contributor III
Neil, thanks so much, It's good news that the answers provided are actually starting to make sense to me.  This was very helpful.
0 Kudos