Object reference not set to an instance of an object.

VS2008 -and I have decalred a new object

Page 1 of 1

4 Replies - 26377 Views - Last Post: 02 July 2008 - 11:32 PM Rate Topic: -----

#1 HiArt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 14-April 08

Object reference not set to an instance of an object.

Posted 02 July 2008 - 02:49 PM

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:

	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:

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:

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: 02 July 2008 - 02:52 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Object reference not set to an instance of an object.

#2 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2257
  • View blog
  • Posts: 9,445
  • Joined: 29-May 08

Re: Object reference not set to an instance of an object.

Posted 02 July 2008 - 03:42 PM

It think this maybe down to a Equals instead of =

View PostHiArt, on 2 Jul, 2008 - 10:49 PM, said:

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
bfLogoff.header.sessionToken = mSessionToken


This post has been edited by AdamSpeight2008: 02 July 2008 - 03:47 PM

Was This Post Helpful? 0
  • +
  • -

#3 HiArt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 14-April 08

Re: Object reference not set to an instance of an object.

Posted 02 July 2008 - 04:17 PM

No mate, makes not a scrap of difference either way!
Was This Post Helpful? 0
  • +
  • -

#4 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Object reference not set to an instance of an object.

Posted 02 July 2008 - 06:19 PM

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.
Was This Post Helpful? 0
  • +
  • -

#5 HiArt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 14-April 08

Re: Object reference not set to an instance of an object.

Posted 02 July 2008 - 11:32 PM

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: 03 July 2008 - 03:49 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1