Welcome to Dream.In.Code
Getting VB.NET Help is Easy!

Join 109,493 VB.NET Programmers for FREE! Ask your question and get quick answers from experts. There are 1,189 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Object reference not set to an instance of an object.

 
Reply to this topicStart new topic

Object reference not set to an instance of an object., VS2008 -and I have decalred a new object

HiArt
post 2 Jul, 2008 - 02:49 PM
Post #1


New D.I.C Head

*
Joined: 14 Apr, 2008
Posts: 9

Hi folks,

I just know this is something simple and obvious, but it is doing my head in and I cannot see the problem for love or money.

Any suggestions gratefully received!

The BFLogin code works fine and displays the sessiontoken

The BFLogout code generates the error at "bfLogoff.header.sessionToken.Equals(mSessionToken)". As you can see from the code below, I have "Dim bfLogoff As New LogoutReq", and a similar line works for the login.

I am using a trail version of VS Team System 2008.

Thanks for the help

Art.

THE CODE

The routines are invoked as follows:

CODE

    Private myBF As New clBetfair                   'Access betfair functions


   Private Sub BetfairLoginToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BetfairLoginToolStripMenuItem.Click

        myBF.BFLogin()

    End Sub

    Private Sub BetfairLogoutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BetfairLogoutToolStripMenuItem.Click

        myBF.BFLogout()

    End Sub


The clBetfair class:

CODE

Option Explicit On

'Contains the code to use the Betfair API
'
'Betfair namespaces added via project/add service references for
'https://api.betfair.com/exchange/v5/BFExchangeService.wsdl
'and https://api.betfair.com/global/v3/BFGlobalService.wsdl.
'
'See http://bdp.betfair.com/index.php?option=com_content&task=blogcategory&id=0&Itemid=62
'
'dd-mm-yy Comment
'======== ================================================================================
=
'12-06-08 Inital version

Imports System
Imports System.Windows.Forms                        'For the message Box
Imports The_Odds_Collector.ServiceReference1        'Betfair's global API
'Imports The_Odds_Collector.ServiceReference2        'API to access UK Sports

Public Class clBetfair
    Private Shared mSessionToken As String

    Public Sub New()
        MyBase.New()
    End Sub

    Public Sub BFLogin()
        'Logon to betfair
        '
        'Parameters()
        'Name       Direction   Type                Comment
        '========== =========== ===========         ===============================================
        'None.

        Dim bfService As New BFGlobalServiceClient
        Dim bflogon As New LoginReq
        Dim resp As New LoginResp
        Dim sMessage As String = ""

        Try
            bflogon.username = <A VALID USERNAME GOES HERE.
            bflogon.password = <A VALID PASSWORD GOES HERE>
            bflogon.productId = 82
            bflogon.vendorSoftwareId = 0

            resp = bfService.login(bflogon)

            If resp.errorCode <> LoginErrorEnum.OK Then
                Select Case resp.errorCode
                    Case LoginErrorEnum.LOGIN_FAILED_ACCOUNT_LOCKED
                        sMessage = "LOGIN_FAILED_ACCOUNT_LOCKED"
                    Case LoginErrorEnum.ACCOUNT_CLOSED
                        sMessage = "ACCOUNT_CLOSED"
                    Case LoginErrorEnum.ACCOUNT_SUSPENDED
                        sMessage = "ACCOUNT_SUSPENDED"
                    Case LoginErrorEnum.API_ERROR
                        sMessage = "API_ERROR "
                        Select Case resp.header.errorCode
                            Case APIErrorEnum.EXCEEDED_THROTTLE
                                sMessage &= "EXCEEDED_THROTTLE"
                            Case APIErrorEnum.INTERNAL_ERROR
                                sMessage &= "INTERNAL_ERROR"
                            Case APIErrorEnum.NO_SESSION
                                sMessage &= "NO_SESSION"
                            Case APIErrorEnum.PRODUCT_REQUIRES_FUNDED_ACCOUNT
                                sMessage &= "PRODUCT_REQUIRES_FUNDED_ACCOUNT"
                            Case APIErrorEnum.SERVICE_NOT_AVAILABLE_FOR_LOGIN_STATUS
                                sMessage &= "SERVICE_NOT_AVAILABLE_FOR_LOGIN_STATUS"
                            Case APIErrorEnum.OK
                                sMessage &= "OK"
                            Case APIErrorEnum.SERVICE_NOT_AVAILABLE_IN_PRODUCT
                                sMessage &= "SERVICE_NOT_AVAILABLE_IN_PRODUCT"
                            Case APIErrorEnum.SUBSCRIPTION_INACTIVE_OR_SUSPENDED
                                sMessage &= "SUBSCRIPTION_INACTIVE_OR_SUSPENDED"
                            Case APIErrorEnum.TOO_MANY_REQUESTS
                                sMessage &= "TOO_MANY_REQUESTS"
                            Case APIErrorEnum.USER_NOT_SUBSCRIBED_TO_PRODUCT
                                sMessage &= "USER_NOT_SUBSCRIBED_TO_PRODUCT"
                            Case APIErrorEnum.VENDOR_SOFTWARE_INACTIVE
                                sMessage &= "VENDOR_SOFTWARE_INACTIVE"
                            Case APIErrorEnum.VENDOR_SOFTWARE_INVALID
                                sMessage &= "VENDOR_SOFTWARE_INVALID"
                        End Select
                    Case LoginErrorEnum.FAILED_MESSAGE
                        sMessage = "FAILED_MESSAGE"
                    Case LoginErrorEnum.INVALID_LOCATION
                        sMessage = "INVALID_LOCATION"
                    Case LoginErrorEnum.INVALID_PRODUCT
                        sMessage = "INVALID_PRODUCT"
                    Case LoginErrorEnum.INVALID_USERNAME_OR_PASSWORD
                        sMessage = "INVALID_USERNAME_OR_PASSWORD"
                    Case LoginErrorEnum.INVALID_VENDOR_SOFTWARE_ID
                        sMessage = "INVALID_VENDOR_SOFTWARE_ID"
                    Case LoginErrorEnum.LOGIN_FAILED_ACCOUNT_LOCKED
                        sMessage = "LOGIN_FAILED_ACCOUNT_LOCKED"
                    Case LoginErrorEnum.LOGIN_REQUIRE_TERMS_AND_CONDITIONS_ACCEPTANCE
                        sMessage = "LOGIN_REQUIRE_TERMS_AND_CONDITIONS_ACCEPTANCE"
                    Case LoginErrorEnum.LOGIN_RESTRICTED_LOCATION
                        sMessage = "LOGIN_RESTRICTED_LOCATION"
                    Case LoginErrorEnum.LOGIN_UNAUTHORIZED
                        sMessage = "LOGIN_UNAUTHORIZED"
                    Case LoginErrorEnum.OK_MESSAGES
                        sMessage = "OK_MESSAGES"
                    Case LoginErrorEnum.POKER_T_AND_C_ACCEPTANCE_REQUIRED
                        sMessage = "POKER_T_AND_C_ACCEPTANCE_REQUIRED"
                    Case LoginErrorEnum.T_AND_C_ACCEPTANCE_REQUIRED
                        sMessage = "T_AND_C_ACCEPTANCE_REQUIRED"
                    Case LoginErrorEnum.USER_NOT_ACCOUNT_OWNER
                        sMessage = "USER_NOT_ACCOUNT_OWNER"
                End Select

                Call UnexpectedError("Unexpected error in clBetfair.BFLogin ", sMessage, "The Odds Collector")
                End
            End If

            MessageBox.Show(resp.header.sessionToken)
            mSessionToken = resp.header.sessionToken

        Catch ex As Exception
            'Deal with unexpected error
            Call UnexpectedError("Unexpected error in clBetfair.BFLogin ", ex.Message, ex.Source)
        End Try

    End Sub

    Public Sub BFLogout()
        'Logout of Betfair
        '
        'Parameters()
        'Name       Direction   Type                Comment
        '========== =========== ===========         ===============================================
        'None.


        Try
            Dim bfService As New BFGlobalServiceClient
            Dim bfLogoff As New LogoutReq
            Dim resp As New LogoutResp
            Dim sMessage As String = ""

            bfLogoff.header.sessionToken.Equals(mSessionToken)

            resp = bfService.logout(bfLogoff)

            If resp.errorCode <> LogoutErrorEnum.OK Then
                Select Case resp.errorCode
                    Case LogoutErrorEnum.API_ERROR
                        sMessage = "API_ERROR "
                        Select Case resp.header.errorCode
                            Case APIErrorEnum.EXCEEDED_THROTTLE
                                sMessage &= "EXCEEDED_THROTTLE"
                            Case APIErrorEnum.INTERNAL_ERROR
                                sMessage &= "INTERNAL_ERROR"
                            Case APIErrorEnum.NO_SESSION
                                sMessage &= "NO_SESSION"
                            Case APIErrorEnum.PRODUCT_REQUIRES_FUNDED_ACCOUNT
                                sMessage &= "PRODUCT_REQUIRES_FUNDED_ACCOUNT"
                            Case APIErrorEnum.SERVICE_NOT_AVAILABLE_FOR_LOGIN_STATUS
                                sMessage &= "SERVICE_NOT_AVAILABLE_FOR_LOGIN_STATUS"
                            Case APIErrorEnum.OK
                                sMessage &= "OK"
                            Case APIErrorEnum.SERVICE_NOT_AVAILABLE_IN_PRODUCT
                                sMessage &= "SERVICE_NOT_AVAILABLE_IN_PRODUCT"
                            Case APIErrorEnum.SUBSCRIPTION_INACTIVE_OR_SUSPENDED
                                sMessage &= "SUBSCRIPTION_INACTIVE_OR_SUSPENDED"
                            Case APIErrorEnum.TOO_MANY_REQUESTS
                                sMessage &= "TOO_MANY_REQUESTS"
                            Case APIErrorEnum.USER_NOT_SUBSCRIBED_TO_PRODUCT
                                sMessage &= "USER_NOT_SUBSCRIBED_TO_PRODUCT"
                            Case APIErrorEnum.VENDOR_SOFTWARE_INACTIVE
                                sMessage &= "VENDOR_SOFTWARE_INACTIVE"
                            Case APIErrorEnum.VENDOR_SOFTWARE_INVALID
                                sMessage &= "VENDOR_SOFTWARE_INVALID"
                        End Select
                End Select

                Call UnexpectedError("Unexpected error in clBetfair.BFLogoff ", sMessage, "The Odds Collector")
                End
            End If

            MessageBox.Show("Logged Out")

        Catch ex As Exception
            'Deal with unexpected error
            Call UnexpectedError("Unexpected error in clBetfair.BFLogoff ", ex.Message, ex.Source)
        End Try

    End Sub

    Private Sub UnexpectedError(ByVal strComment As String, _
                                 ByVal strDescription As String, _
                                 ByVal strSource As String)
        On Error Resume Next

        Dim strMessage

        strMessage = strComment & vbCrLf & vbCrLf & _
                     "Run-time error '" & strDescription

        MessageBox.Show(strMessage, strSource, MessageBoxButtons.OK, MessageBoxIcon.Error)

        Debug.Print(strSource & ": " & strComment & " - " & strDescription)

        Debug.Assert(False)
    End Sub

End Class


THE ERROR:

CODE

A first chance exception of type 'System.NullReferenceException' occurred in OddsColl.exe
OddsColl: Unexpected error in clBetfair.BFLogoff  - Object reference not set to an instance of an object.



This post has been edited by HiArt: 2 Jul, 2008 - 02:52 PM
User is offlineProfile CardPM

Go to the top of the page


AdamSpeight2008
post 2 Jul, 2008 - 03:42 PM
Post #2


D.I.C Addict

Group Icon
Joined: 29 May, 2008
Posts: 506



Thanked 34 times

Dream Kudos: 1900
My Contributions


It think this maybe down to a Equals instead of =

QUOTE(HiArt @ 2 Jul, 2008 - 10:49 PM) *

The BFLogout code generates the error at "bfLogoff.header.sessionToken.Equals(mSessionToken)". As you can see from the code


My guess it should have been
vb

bfLogoff.header.sessionToken = mSessionToken


This post has been edited by AdamSpeight2008: 2 Jul, 2008 - 03:47 PM
User is offlineProfile CardPM

Go to the top of the page

HiArt
post 2 Jul, 2008 - 04:17 PM
Post #3


New D.I.C Head

*
Joined: 14 Apr, 2008
Posts: 9

No mate, makes not a scrap of difference either way!
User is offlineProfile CardPM

Go to the top of the page

jayman9
post 2 Jul, 2008 - 06:19 PM
Post #4


Student of Life

Group Icon
Joined: 26 Dec, 2005
Posts: 6,323



Thanked 22 times

Dream Kudos: 500

Expert In: C#, VB.NET, Java

My Contributions


Are you certain that mSessionToken has a value?

Adam is correct if your intention is to assign the value from mSessionToken to bfLogoff.header.sessionToken.Equals(mSessionToken). Then the statement is incorrect, the Equals method is used to compare two objects, not assign a value to an object.
User is offlineProfile CardPM

Go to the top of the page

HiArt
post 2 Jul, 2008 - 11:32 PM
Post #5


New D.I.C Head

*
Joined: 14 Apr, 2008
Posts: 9

I don't understand it! I had tried .equals and "=" and changed nothing else (I promise) and still got the error. This morning I changed the .equals back to "=" and it works.

Anyway, Adam & Jayman9, many (many, many) thanks for the help.

Just to be absolutely clear

bfLogoff.header.sessionToken.equals(mSessionToken)

becomes

bfLogoff.header = new apisessionheader
bfLogoff.header.sessionToken = mSessionToken

I wasn't creating the apisessionheader instance, and that is what i believe was throwing the error. I assumed that as the apisessionheader is part of the LoginReq class it would be created in the "New LoginReq" statement. WRONG!

Either way, it works now so i am a happy bunny.

This post has been edited by HiArt: 3 Jul, 2008 - 03:49 AM
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 9/7/08 02:17PM

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month