Not set to instance of object for Service Reference

GrexD

Well-known member
Joined
Mar 5, 2008
Messages
95
Programming Experience
Beginner
I've programmed to two other service references, but it is still a little new to me. I received the WDSL and loaded the reference and the objects were created. I can see everything in the object browser. This is in VS 2010. Before I tried this in 2010 I accessed the service with soupUI 3.5, so I'm sure service works. In soapUI I did have to add in my p12 certificate, which is something I've never worked with in VS 2010, but I'm not sure if that is what is causing my problem. It might be, but I'm just not sure. It seems I should be able to create all of the objects, but if it was the cert it would fail at a later point.

I create 3 objects defined by the WDSL. There is a request object, a response object and a ServiceClient.

VB.NET:
Dim NewRequest As New MCPRequest
NewRequest.PayLoad.EBSHeader.clientID = "XX-XXXXX"

On the second line I get an exception that says, "Object reference not set to an instance of an object". If I comment out the 'NewRequest.PayLoad.EBSHeader.clientID = "XX-XXXXX"' the rest of the code runs without exception, including the other 2 WDSL objects. I have the New keyword in there and 'MCPRequest' is in cyan. I use Option Explicit On and Option Strict On at the top and the root service reference has an Imports call.

Any idea what I'm doing wrong?
Greg
 
This doesn't really have anything to do with the fact that it's a service reference. You are creating a MCPRequest object so you know that NewRequest is not Nothing. That code doesn't set the PayLoad property of that object though, so unless it's set internally NewRequest.PayLoad will be Nothing. Likewise, even if PayLoad is not Nothing, that code doesn't set the EBSHeader property of that object, so it will be Nothing unless it's set internally. This is yet another example of how programming objects mimic real-world objects. If I told you to go and get my dog's collar, how exactly would you do that if I don't have a dog?
Dim theCollar = Me.Dog.Collar
That code will throw an exception because Me.Dog is Nothing and you can't get the collar of a dog that doesn't exist.
 
Thanks. That got it. I was treating those like properties. I'm now getting a reject message from the server, so the code seems to be working, but just not the way I want it. Do you know of any good posts or articles dealing with using certificates in vb.net and a service reference. I have the code below which seems to be loaded the proper cert, but I'm not sure what to do with it, if anything.

VB.NET:
            Dim MyCred As New ServiceModel.Description.ClientCredentials()
            MyCred.ClientCertificate.SetCertificate(StoreLocation.CurrentUser, StoreName.TrustedPublisher, X509FindType.FindBySerialNumber, "xx xx xx xx xx xx xx")


Thank you,

Greg
 
Back
Top