Object Reference Not Set to an Instance of an Object error when calling unmanaged DLL function

Object Reference Not Set to an Instance of an Object error when calling unmanaged DLL function

Post by John Shoebro » Sat, 11 Jan 2003 02:11:52



I have searched the world over (well, Google and MSDN anyway) for
solutions to this problem and have been unsuccessful in my attempts.
The following is the VB.NET code I am using to define the structures,
declare the unmanaged DLL function and call the unmanaged DLL
function.  The line that calls the function results in the "Object
reference not set to an instance of an object" error.  Any help on
this would be tremendously appreciated.

    ' The MATCHINPUT function parameter for MatchAddress()
    <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)>
Public Structure MATCHINPUT
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Dim cFunc
As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Dim
cStreetStrict As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Dim
cFirmStrict As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Dim
cDirStrict As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Dim
cNormAddr As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Dim
cTestMode As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Dim
cVanCityNam As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Dim
cDualAddr As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Dim cCasing
As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Dim cLOT As
String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Dim
cHRDefMatch As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Dim
cSecCompMatch As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Dim
cPrefCityNam As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Dim cDPV As
String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Dim
cDPVZIPPOB As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Dim
cDPVZIPAddr As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Dim cDPVCR
As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Dim cDPVDir
As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Dim cDPVSfx
As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Dim
cDPVSfxDir As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Dim
cDPVSecComp As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Dim
cDPVUnique As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Dim
cAuxMatch As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=6)> Dim sZIP As
String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=5)> Dim sZIP4
As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=101)> Dim
sPriAddr As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=101)> Dim
sSecAddr As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=101)> Dim
sFirmName As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=101)> Dim
sCitySt As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=36)> Dim
sUrbName As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Dim
cEnviron As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=11)> Dim sDBLib
As String

    End Structure

    <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)>
Public Structure MULTIANSWER

        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Dim cRC As
String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Dim
cProbCorr As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Dim
cUSPSRecType As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Dim
cDefMatch As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=65)> Dim
sStdAddr As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=3)> Dim
sStdAddrLen As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=11)> Dim
sPriHseRngLow As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=11)> Dim
sPriHseRngHi As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Dim
cPriOddEven As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=9)> Dim
sSecHseRngLow As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=9)> Dim
sSecHseRngHi As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Dim
cSecOddEven As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=41)> Dim
sFirmName As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=29)> Dim
sLongCityNam As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=14)> Dim
sShortCityNam As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=29)> Dim
sUrbanNam As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=3)> Dim
sStateAbbr As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=6)> Dim sZIP As
String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=5)> Dim sZIP4
As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=3)> Dim
sDPBCAddOn As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Dim
cDPBCChkDigit As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=5)> Dim sCRC As
String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=4)> Dim
sCountyNum As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=3)> Dim
sCongressDist As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=7)> Dim
sLastLineNum As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=7)> Dim sFinNum
As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Dim cScore
As String

    End Structure

    ' The MATCHOUTPUT function parameter for MatchAddress()

    <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)>
Public Structure MATCHOUTPUT

        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Dim cGenRC
As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Dim
cMatProbCorr As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Dim
cMatScore As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Dim cDirRC
As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Dim cSfxRC
As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Dim cAptRC
As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=5)> Dim
sMatAttempts As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Dim
cAddrAptNum As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Dim
cAddrProbCorr As String
        Dim AddrLine0 As MULTIANSWER
        Dim AddrLine1 As MULTIANSWER
        Dim AddrLine2 As MULTIANSWER
        Dim AddrLine3 As MULTIANSWER
        Dim AddrLine4 As MULTIANSWER
        Dim AddrLine5 As MULTIANSWER
        Dim AddrLine6 As MULTIANSWER
        Dim AddrLine7 As MULTIANSWER
        Dim AddrLine8 As MULTIANSWER
        Dim AddrLine9 As MULTIANSWER

    End Structure

    Public Class CODE1

        <DllImport("C1IAPI")> Public Shared Function MatchAddress
                             (ByVal stMatchIn As MATCHINPUT, _
                              ByRef stMatchOut As MATCHOUTPUT, _
                              ByVal lConvertStrings As Long) As Long

        End Function

    End Class

    Private Sub ScrubAddress(ByRef AddressLine1 As String, _
                             ByRef AddressLine2 As String, _
                             ByRef AddressCity As String, _
                             ByRef StateCD As String, _
                             ByRef ZipCode As String, _
                             ByVal AddressFormat As
fldAddressFormatCode)

        Dim sMatchIn As New MATCHINPUT()
        Dim sMatchOut As New MATCHOUTPUT()
        Dim lRet As Long

        Try

            ' Set input fields for Code-1 MatchAddress function

            sMatchIn.cFunc = "5"
            sMatchIn.cStreetStrict = "L"
            sMatchIn.cFirmStrict = "M"
            sMatchIn.cDirStrict = "M"
            sMatchIn.cNormAddr = "N"
            sMatchIn.cVanCityNam = "X"
            sMatchIn.cCasing = "C"
            sMatchIn.sZIP = Trim(ZipCode)
            sMatchIn.sPriAddr = Trim(AddressLine1)
            sMatchIn.sSecAddr = Trim(AddressLine2)
            sMatchIn.sCitySt = Trim(AddressCity) & " " & Trim(StateCD)
            sMatchIn.cEnviron = "I"

            ' Scrub the address using the Code-1 MatchAddress function

            lRet = CODE1.MatchAddress(sMatchIn, sMatchOut, 1)
            Line where the error occurs

        Catch e As Exception
            Throw e
        End Try

    End Sub

 
 
 

Object Reference Not Set to an Instance of an Object error when calling unmanaged DLL function

Post by Phil Wilso » Sat, 11 Jan 2003 04:16:48


You've put MatchAddress in a class which has never been instantiated. You're mimicing the C# way of
doing this with static methods etc. I am no VB.Net expert, but I don't think MatchAddress needs to
be in a class unless you have some reason for having a class. Just declare MatchAddress on its own
and use DllImport in front of it.

"John Shoebrook" <jshoebr...@covansys.com> wrote in message

news:5c310ec3.0301090911.336c13f@posting.google.com...
> I have searched the world over (well, Google and MSDN anyway) for
> solutions to this problem and have been unsuccessful in my attempts.
> The following is the VB.NET code I am using to define the structures,
> declare the unmanaged DLL function and call the unmanaged DLL
> function.  The line that calls the function results in the "Object
> reference not set to an instance of an object" error.  Any help on
> this would be tremendously appreciated.

>     ' The MATCHINPUT function parameter for MatchAddress()
>     <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)>
> Public Structure MATCHINPUT
>         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Dim cFunc
> As String
>         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Dim
> cStreetStrict As String
>         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Dim
> cFirmStrict As String
>         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Dim
> cDirStrict As String
>         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Dim
> cNormAddr As String
>         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Dim
> cTestMode As String
>         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Dim
> cVanCityNam As String
>         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Dim
> cDualAddr As String
>         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Dim cCasing
> As String
>         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Dim cLOT As
> String
>         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Dim
> cHRDefMatch As String
>         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Dim
> cSecCompMatch As String
>         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Dim
> cPrefCityNam As String
>         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Dim cDPV As
> String
>         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Dim
> cDPVZIPPOB As String
>         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Dim
> cDPVZIPAddr As String
>         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Dim cDPVCR
> As String
>         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Dim cDPVDir
> As String
>         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Dim cDPVSfx
> As String
>         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Dim
> cDPVSfxDir As String
>         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Dim
> cDPVSecComp As String
>         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Dim
> cDPVUnique As String
>         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Dim
> cAuxMatch As String
>         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=6)> Dim sZIP As
> String
>         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=5)> Dim sZIP4
> As String
>         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=101)> Dim
> sPriAddr As String
>         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=101)> Dim
> sSecAddr As String
>         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=101)> Dim
> sFirmName As String
>         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=101)> Dim
> sCitySt As String
>         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=36)> Dim
> sUrbName As String
>         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Dim
> cEnviron As String
>         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=11)> Dim sDBLib
> As String

>     End Structure

>     <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)>
> Public Structure MULTIANSWER

>         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Dim cRC As
> String
>         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Dim
> cProbCorr As String
>         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Dim
> cUSPSRecType As String
>         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Dim
> cDefMatch As String
>         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=65)> Dim
> sStdAddr As String
>         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=3)> Dim
> sStdAddrLen As String
>         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=11)> Dim
> sPriHseRngLow As String
>         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=11)> Dim
> sPriHseRngHi As String
>         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Dim
> cPriOddEven As String
>         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=9)> Dim
> sSecHseRngLow As String
>         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=9)> Dim
> sSecHseRngHi As String
>         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Dim
> cSecOddEven As String
>         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=41)> Dim
> sFirmName As String
>         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=29)> Dim
> sLongCityNam As String
>         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=14)> Dim
> sShortCityNam As String
>         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=29)> Dim
> sUrbanNam As String
>         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=3)> Dim
> sStateAbbr As String
>         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=6)> Dim sZIP As
> String
>         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=5)> Dim sZIP4
> As String
>         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=3)> Dim
> sDPBCAddOn As String
>         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Dim
> cDPBCChkDigit As String
>         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=5)> Dim sCRC As
> String
>         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=4)> Dim
> sCountyNum As String
>         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=3)> Dim
> sCongressDist As String
>         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=7)> Dim
> sLastLineNum As String
>         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=7)> Dim sFinNum
> As String
>         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Dim cScore
> As String

>     End Structure

>     ' The MATCHOUTPUT function parameter for MatchAddress()

>     <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)>
> Public Structure MATCHOUTPUT

>         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Dim cGenRC
> As String
>         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Dim
> cMatProbCorr As String
>         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Dim
> cMatScore As String
>         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Dim cDirRC
> As String
>         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Dim cSfxRC
> As String
>         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Dim cAptRC
> As String
>         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=5)> Dim
> sMatAttempts As String
>         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Dim
> cAddrAptNum As String
>         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Dim
> cAddrProbCorr As String
>         Dim AddrLine0 As MULTIANSWER
>         Dim AddrLine1 As MULTIANSWER
>         Dim AddrLine2 As MULTIANSWER
>         Dim AddrLine3 As MULTIANSWER
>         Dim AddrLine4 As MULTIANSWER
>         Dim AddrLine5 As MULTIANSWER
>         Dim AddrLine6 As MULTIANSWER
>         Dim AddrLine7 As MULTIANSWER
>         Dim AddrLine8 As MULTIANSWER
>         Dim AddrLine9 As MULTIANSWER

>     End Structure

>     Public Class CODE1

>         <DllImport("C1IAPI")> Public Shared Function MatchAddress
>                              (ByVal stMatchIn As MATCHINPUT, _
>                               ByRef stMatchOut As MATCHOUTPUT, _
>                               ByVal lConvertStrings As Long) As Long

>         End Function

>     End Class

>     Private Sub ScrubAddress(ByRef AddressLine1 As String, _
>                              ByRef AddressLine2 As String, _
>                              ByRef AddressCity As String, _
>                              ByRef StateCD As String, _
>                              ByRef ZipCode As String, _
>                              ByVal AddressFormat As
> fldAddressFormatCode)

>         Dim sMatchIn As New MATCHINPUT()
>         Dim sMatchOut As New MATCHOUTPUT()
>         Dim lRet As Long

>         Try

>             ' Set input fields for Code-1 MatchAddress function

>             sMatchIn.cFunc = "5"
>             sMatchIn.cStreetStrict = "L"
>             sMatchIn.cFirmStrict = "M"
>             sMatchIn.cDirStrict = "M"
>             sMatchIn.cNormAddr = "N"
>             sMatchIn.cVanCityNam = "X"
>             sMatchIn.cCasing = "C"
>             sMatchIn.sZIP = Trim(ZipCode)
>             sMatchIn.sPriAddr = Trim(AddressLine1)
>             sMatchIn.sSecAddr = Trim(AddressLine2)
>             sMatchIn.sCitySt = Trim(AddressCity) & " " & Trim(StateCD)
>             sMatchIn.cEnviron = "I"

>             ' Scrub the address using the Code-1 MatchAddress function

>             lRet = CODE1.MatchAddress(sMatchIn, sMatchOut, 1)
>             ' Line where the error occurs

>         Catch e As Exception
>             Throw e
>         End Try

>     End Sub