Search code examples
c#.netserviceenterprise

Object reference not instantiated after instantiation (consuming WS of SAP Enterprise Service Workspace)


I am trying to consume some of these services in .Net/C#. Some of the Service could be easily consumed but with others I have got a misleading error.

This a part of the code:

input.AcademicProgramOfStudySelectionByName = new AcademicProgramOfStudyByNameQueryMessage_syncAcademicProgramOfStudySelectionByName();
input.AcademicProgramOfStudySelectionByName.AcademicProgramOfStudyName.languageCode = "DE";

I am getting at the second line the error

Object reference not set to an object instance

but I have created the object in the first line!

The same code works in some other service!


Solution

  • you created the object input.AcademicProgramOfStudySelectionByName, but you did not create its member input.AcademicProgramOfStudySelectionByName.AcademicProgramOfStudyName. As it seems, the constructor of class AcademicProgramOfStudyByNameQueryMessage_syncAcademicProgramOfStudySelectionByName does not populate its member AcademicProgramOfStudyName. So when you try to assign a value to a member of AcademicProgramOfStudyName and that instance is NULL, you get an exception.

    Example code:

        AcademicProgramOfStudyByNameQueryResponse_InClient client = 
            new AcademicProgramOfStudyByNameQueryResponse_InClient(); 
    
    
        client.ClientCredentials.UserName.UserName = "XX";
        client.ClientCredentials.UserName.Password = "YY";
    
        AcademicProgramOfStudyByNameQueryMessage_sync input =
            new AcademicProgramOfStudyByNameQueryMessage_sync();
    
    
        input.AcademicProgramOfStudySelectionByName = new AcademicProgramOfStudyByNameQueryMessage_syncAcademicProgramOfStudySelectionByName();
    
        // this is the member that currently is still NULL and has to be created:
        input.AcademicProgramOfStudySelectionByName.AcademicProgramOfStudyName = new <insert whatever class is needed here>
    
        // now this should work without throwing an exception
       input.AcademicProgramOfStudySelectionByName.AcademicProgramOfStudyName.languageCode = "DE";
    
        AcademicProgramOfStudyByNameResponseMessage_sync output = 
            new AcademicProgramOfStudyByNameResponseMessage_sync(); 
    
        output = client.AcademicProgramOfStudyByNameQueryResponse_In(input);