Problem with BEmu.Event

Jul 18, 2013 at 2:16 PM
Hi, I'm using BEmu to test a simple case based on the examples given in the API docs.

Here is the problem code:

bool continueToLoop = true;
while (continueToLoop)
{
Event eventObj = session.NextEvent();
switch (eventObj.Type)
{
case Event.EventType.RESPONSE: // final event
continueToLoop = false;
handleResponseEvent(eventObj);
break;
case Event.EventType.PARTIAL_RESPONSE:
handleResponseEvent(eventObj);
break;
default:
handleOtherEvent(eventObj);
break;
}
}

When I run the program I get NullRefException on the line:
Event eventObj = session.NextEvent();

I'm assuming this has something to do with Event being an abstract class. I have debugged and checked, session is not null, so there shouldn't be any reason I can't assign eventObj, even if for some reason it was to null.
Jul 18, 2013 at 2:49 PM
Edited Jul 19, 2013 at 1:33 PM
Exception Info:

NullReferenceException was unhandled.
Object reference not set to an instance of an object.

System.NullReferenceException was unhandled
HResult=-2147467261
Message=Object reference not set to an instance of an object.
Source=BEmu
Coordinator
Jul 18, 2013 at 6:43 PM
Thank you for contacting me over this. Would you please send me or paste in a C# file that causes this exception. I suspect that the offending code may come somewhere before the code you pasted. For example, I am able to cause a NullReferenceException if I try to append a null to the "fields" part of a Reference Data Request. The full code that causes that exception is:
    using BEmu;
    static void Main(string[] args)
    {
        SessionOptions sessionOptions = new SessionOptions();
        sessionOptions.ServerHost = "127.0.0.1";
        sessionOptions.ServerPort = 8194;

        Session session = new Session(sessionOptions);
        if (!session.Start())
        {
            System.Console.WriteLine("Could not start session.");
            System.Environment.Exit(1);
        }
        if (!session.OpenService("//blp/refdata"))
        {
            System.Console.WriteLine("Could not open service //blp/refdata");
            System.Environment.Exit(1);
        }
        CorrelationID requestID = new CorrelationID(1);
        Service refDataSvc = session.GetService("//blp/refdata");
        Request request = refDataSvc.CreateRequest("ReferenceDataRequest");

        request.Append("securities", "IBM US EQUITY");
        request.Append("fields", null); //this is the problem

        session.SendRequest(request, new CorrelationID(-999));

        bool continueToLoop = true;
        while (continueToLoop)
        {
            Event eventObj = session.NextEvent(); //the exception is raised here
            switch (eventObj.Type)
            {
                case Event.EventType.RESPONSE: // final event 
                    continueToLoop = false;
                    handleEvent(eventObj);
                    break;
                case Event.EventType.PARTIAL_RESPONSE:
                    handleEvent(eventObj);
                    break;
            }
        }
        Console.ReadLine();
    }

    private static void handleEvent(Event eventObj)
    {
        foreach (var item in eventObj.GetMessages())
        {
            Console.WriteLine(item);
        }
    }
In my code, look for my comment "//this is the problem". My code doesn't handle a "null" passed as an argument at that line; however the exception is raised on a following line (look for the comment "//the exception is raised here").
Jul 19, 2013 at 1:33 PM
Thank you for your response. The problem you demonstrated was exactly the issue. I was attempting to put something together very quickly as POC but neglected proper error checking.

But, I do have another question. Is it possible to intentionally get errors from the emulator? I want to test some code that I use upon recieving a securityError (as outlined in the BBAPI docs), but the emulator returns data regardless of the security name supplied. (This is all using //blp/refdata as in your code above.)

Thank again for the help.
Coordinator
Jul 19, 2013 at 7:48 PM
Do you want an error message in the event that you requested an invalid security? That's actually a limitation of my code. Given the universe of securities available in Bloomberg, I could never know which string of characters is a valid or invalid security. Would it be helpful if I included support for a security that definitely doesn't exist? Like "ZYZZ US EQUITY". I could make it so that if you request this security, you get an error message every time.

If you are asking about fields, my reply is the same. There must be thousands of fields. I can do the same thing for a field and call it something like "undefined".

Is this what you're asking about?