So, I'm following the "Walkthrough: Migrating VB6 to VB.NET for ArcGIS 10" to convert my VB code to .NET (using Visual Studio 2010), and I'm running into this error, "NullReferenceException was unhandled by user code", object reference not set to an instance of an object. It suggests that I use a "new" keyword to create an object instance. I imported all my namespaces under Option Explicit (for this example the GeoDatabase namespace was imported), and turned Option Strict On, and applied all the CType conversions that VS required. Since this walkthrough example is for ArcGIS 10, and I'm using 9.3.1, could this be the issue? I tried to pass the "new" keyword below, but I'm still getting the error. Here is the code. Any input will be greatly appreciated.

If SelectionSet.Count < 1 Then
        MsgBox("Please select one or more wells", MsgBoxStyle.Critical, "No Selected Wells")
        Exit Sub
        'Retrieve all features
        'Set pFeatCursor = pFClass.Search(QFilt, False)
        'Retrieve just the selected features
        'This statement moves the selected features into a feature cursor.  All
        'subsequent work is done using the feature cursor.
        'UPGRADE_WARNING: Couldn't resolve default property of object pFeatCursor. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
        SelectionSet.Search(QFilt, False, CType(FeatCursor, ICursor))
    End If

    'Get the first feature
    Dim Feature As IFeature = Nothing 
    Feature = FeatCursor.NextFeature '<<<ERROR

Thank You!


So, I have my ICommand Sub that calls my CreateStick Sub with the new ByRef Cursor As ICursor exception.

 Private Sub ICommand_OnClick() Implements ICommand.OnClick
    End Sub

 Public Sub CreateStick(ByVal QueryFilter As IQueryFilter, ByVal Recycling As Boolean, _
    ByRef Cursor As ICursor)

I stil have two errors on this line:

SelectionSet.Search(QFilt, False, ByRef CType(FeatCursor, ICursor))

ByRef and ICursor are underlined. ByRef = Expression expected and ICursor error = ICursor is a type and cannot be used as an expression.

| improve this question | | | | |
  • What line is causing the NullReferenceException? I expect the 'New Feature' should give you a different error since it's not an type you can "new". – James Schek Apr 8 '11 at 16:47
  • "Feature = FeatCursor.NetFeature" is where the NullReference is occuring. I also just noticed, before the CType update on the "SelectionSet.Search(QFilt, False, CType(FeatCursor, ICursor))" line that "ICursor" was "IFeatureCursor" before. FeatCursor is Dim as IFeatureCursor not ICursor. Maybe this is my problem? I will update my Dim then repost, thanks. – artwork21 Apr 8 '11 at 17:04
  • I re-dimensioned FeatCursor to "Dim FeatCursor as ICursor" and VS gave a late binding error, "Option Strict On disallows late binding." Maybe FeatCursor needs to be Dim as "Dim FeatCursor as IFeatureCursor?" – artwork21 Apr 8 '11 at 17:31
  • Is VB.NET okay with the name 'Feature'? The line 'Feature = New Feature' looks like an accident waiting to happen. – Anthony -GISCOE- Apr 8 '11 at 17:44
  • I removed "Feature = New Feature" and re-named Feature to Feature2 and I'm still getting the same "NullReferenceException was unhandled by user code", object reference not set to an instance of an object error – artwork21 Apr 8 '11 at 18:20

ByRef and ICursor are underlined. ByRef = Expression expected and ICursor error = ICursor is a type and cannot be used as an expression.

When you have the keyword ByRef you can ONLY have a variable following it. What you have following it is a function. ByRef means that the variable you are giving to the function you expect to change in the function and you want to preserve that change outside of the function. You cannot do a CType here because that is its own function.

Instead do your cast first and place that new variable after the ByRef.

Good Luck

| improve this answer | | | | |
  • 1
    +1 I wonder why the compiler isn't smart enough to catch this. – Kirk Kuykendall Apr 11 '11 at 22:16
  • That's the legacy of the old VB, where byref was a default semantics when no modifer was specified. This was changed to byval in VB.NET. The Microsoft's mission to maintain as much code compatibility as possible can easily lead to subtle bugs. Luckily C# came around with a clean slate :-) – Petr Krebs Apr 12 '11 at 6:36

The ArcObjects API states the following for the Search method you used in your example:

[Visual Basic .NET]
Public Sub Search ( _
    ByVal QueryFilter As IQueryFilter, _
    ByVal Recycling As Boolean, _
    ByRef Cursor As ICursor _

Which means that you forgot to supply the needed ByRef keyword for your cursor. Thus it doesn't get assigned and you will get a NullReferenceException when trying to access any method on that empty reference. This should fix your problem:

SelectionSet.Search(QFilt, False, ByRef CType(FeatCursor, ICursor))

EDIT: Edit your code like this:

Dim cursor as ICursor
cursor = FeatCursor
SelectionSet.Search(QFilt, False, ByRef cursor)

PS: Please don't downvote answers which aren't by definition wrong.

| improve this answer | | | | |
  • see my updated question. – artwork21 Apr 11 '11 at 12:42
  • Updated my answer. – AndOne Apr 11 '11 at 15:00
  • So, I can eliminate the ByVal and ByRef info in the parenthesis of the Public Sub CreateStick? – artwork21 Apr 11 '11 at 16:10
  • Yes. Judging on the code you supplied, they aren't necessary at that point. – AndOne Apr 12 '11 at 8:10

I'm not an VB master but will take a poke at this one. At least in the code you've posted, it doesn't like FeatCursor is being initialized, or declared anywhere for that matter.(unless the CType statement does something magical to the variable?). Try 'Dim pFeatCursor as ICursor' before your if/else statement.

| improve this answer | | | | |
  • I'm not sure that I understand you answer. I'm not concerned if the Selection.Count is < 1, I'm trying to fix the "Else" portion of the code. So, why would I need to uncomment "Set pFeatCursor" statement? – artwork21 Apr 11 '11 at 12:54
  • Well your updated question potentially clarifies the situation, but with Option Explicit set, I still don't see where you're actually creating a concrete cursor object for pFeatCursor in this code. Where is the 'Set FeatCursor' statement? – WolfOdrade Apr 11 '11 at 16:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.