Search code examples
c#.netweb-servicesbetfair

C#: Web Service Login is working, but another method is giving me an error


I have the 2 methods below, in a class, The Login Method works fine and retrieves and sets the session token, but in order for me to use GetEvents(), I have to send the sessionToken in the request for GetEvents().

But on the 4th Line of code (excluding Comments & Whitespace) of the getEvents(), I get the error: Object reference not set to an instance of an object.

 The Entire Source can be downloaded here: (Copy and Paste into your browser)
 http://www.theebookzone.co.uk/betfairui.zip

Any Ideas what Im doing wrong?
Any help appreciated, even if its not directly related to this matter.

public static string SessionToken = ""; // Set by Login();

static LoginResp Login()
    {
        // Make a new BFGS instance
        BFGlobal = new BFGlobalService.BFGlobalService();

        // Set up the request in [req]
        LoginReq req = new LoginReq();
        req.username = username;
        req.password = password;
        req.productId = productId;
        req.vendorSoftwareId = softwareId;

        // Set up the response in [resp]
        // Execute the call, and pass in the request
        LoginResp resp = BFGlobal.login(req);

        // Sets our public variable above to the recieved sessionToken
        SessionToken = resp.header.sessionToken;

        // return [resp] - which is the response from the call
        return resp;

    }

    public Array GetEvents()
    {
        // This will set the sessionToken declared at the top.
        LoginToBetfair();

        // Make a new instance of the web service
        BFGlobal = new BFGlobalService.BFGlobalService();

        // Load up the request
        GetEventsReq req = new GetEventsReq();

        // Error Line Below:
        req.header.sessionToken = SessionToken;  // <--- Here is where I get the error
        // Error Above Line: Object reference not set to an instance of an object.

        GetEventsResp resp = BFGlobal.getEvents(req);

        Array marketItems = resp.marketItems;

        return marketItems;

    }

Solution

  • I'd wager that the null object is the header of req.header. Put a break point at that line and then in the variables debugger window see what req.header evaluates to. If it is indeed null then you'll have to manually add the headers with something like

    req.headers = new Headers();