Rida
Rida

Reputation: 23

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!

Upvotes: 2

Views: 194

Answers (2)

Dirk Trilsbeek
Dirk Trilsbeek

Reputation: 6033

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);

Upvotes: 0

Rida
Rida

Reputation: 23

it is really strange. see this compare: In this code i get the error

        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();
        input.AcademicProgramOfStudySelectionByName.AcademicProgramOfStudyName.languageCode = "DE";

        AcademicProgramOfStudyByNameResponseMessage_sync output = 
            new AcademicProgramOfStudyByNameResponseMessage_sync(); 

        output = client.AcademicProgramOfStudyByNameQueryResponse_In(input);

and in this code i dont!!

        CustomerSimpleByNameAndAddressQueryResponse_InClient client =
          new CustomerSimpleByNameAndAddressQueryResponse_InClient();

        client.ClientCredentials.UserName.UserName = "XX";
        client.ClientCredentials.UserName.Password = "YY";

        CustomerSimpleByNameAndAddressQueryMessage_sync input = new CustomerSimpleByNameAndAddressQueryMessage_sync();

        input.CustomerSimpleSelectionByNameAndAddress = new CustomerSimpleByNameAndAddressQueryMessage_syncCustomerSimpleSelectionByNameAndAddress();
        input.CustomerSimpleSelectionByNameAndAddress.CustomerAddressCityName = "Berlin";

        CustomerSimpleByNameAndAddressResponseMessage_sync output = new CustomerSimpleByNameAndAddressResponseMessage_sync();

        output = client.CustomerSimpleByNameAndAddressQueryResponse_In(input);

Upvotes: 0

Related Questions