PLEASE HELP - object reference not set to instance of an object

K

Keith Chadwick

Using MSXML2.XMLHTTP to create a soap package that contains xml in the body
which works perfectly well. But in the web service no matter WHAT I do in
my service to reference the passed xmlnode it returns object not set to an
instance of the node.

This is the soap package I am sending to the service.

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Header></soap:Header>
<soap:Body>
<submitContact xmlns="evententerprise">
<data>
<contact address="asdfasdfasdf">
<Test>this is a test</Test>
</contact>
</data>
</submitContact>
</soap:Body>
</soap:Envelope>


My web service basically is as follows:

<WebMethod(EnableSession:=True)> _
Public Function submitContact(ByVal data As XmlNode) As String
Dim result As String
Dim xe As XmlElement
Try
xe = data.SelectSingleNode("//Test")
result = xe.Value
Catch ex As Exception
result = ex.Message
End Try
Return result
End Function

I have even tried place the data.outerXml into a new xmldocument and trying
to reference that but no go. No matter what I do it comes back with the
exception message 'Object reference not set to an instance of an object'

Please help am going nuts trying to figure out what is going on!!

Cheers
Keith
 
K

Keith Chadwick

When I look at my resulting xml that is being loaded into the xmldocument
within the webservice I am seeing the following:

<contact xmlns="evententerprise">
<Title xmlns="evententerprise">Mr</Title>
<Fname xmlns="evententerprise">Joe</Fname>
<Lname xmlns="evententerprise">Bob</Lname>
</contact>

Is this what is causing me my problems when i try
myxmldoc.selectsinglenode("//Lname")
 
D

Dan Rogers

Are you sure you want //Test? Doesn't that say "Find a node named Test at
the root of the document?" Another thought is you need to accommodate
namespace to get the Xpath expression to find the proper node.

If you serialize the XMl you receive to a file or look at it in the
debugger, are you getting the node you expect?
--------------------
 
K

Keith Chadwick

Been trying everything Dan with no luck.

Have done the following:

Public Function submitContact(ByVal data As XmlNode) As xmldocument
Dim result As String
Dim resultDoc as new XmlDocument

resultDoc.loadxml("<root>" & data.innerXML() & "</root>")

return resultDoc
End

The xml comes back fine the same way I sent it down!!!

But if I do anything like
myElement=resultDoc.selectSingleNode("ROOT/contact/Title") it allways
allways no matter what comes back with the same error that I did not create
a new instance when I try to get at the element.

I tried a test=resultDoc.selectNodes("ROOT/contact/Title").count and it
always comes back as 0 for count. I am missing something obvious here.

In my example above the XML document that is returned to is below:

<?xml version="1.0" encoding="utf-8"?><soap:Envelope
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><submitContactResponse
xmlns="evententerprise"><submitContactResult><ROOT xmlns=""><Title
xmlns="evententerprise">Mr</Title><GivenName
xmlns="evententerprise">Andi</GivenName><MiddleName
xmlns="evententerprise">Thornton</MiddleName><Surname
xmlns="evententerprise">Cooper</Surname><Department
xmlns="evententerprise">department</Department><Assistant
xmlns="evententerprise">Assistants%20Name</Assistant><Manager
xmlns="evententerprise">Managers%20Name</Manager><Profession
xmlns="evententerprise">profession</Profession><JobTitle
xmlns="evententerprise">Job%20Title</JobTitle><EmployeeNumber
xmlns="evententerprise">employee%20number</EmployeeNumber><Company
xmlns="evententerprise">Company</Company><Language
xmlns="evententerprise">English</Language><email1
xmlns="evententerprise">[email protected]</email1><email2
xmlns="evententerprise">[email protected]</email2><email3
xmlns="evententerprise">[email protected]</email3><email
xmlns="evententerprise">email1</email><webpage
xmlns="evententerprise">http://web page address</webpage><BusinessPhone
xmlns="evententerprise">business%20phone</BusinessPhone><HomePhone
xmlns="evententerprise">home%20phone</HomePhone><BusinessFax
xmlns="evententerprise">bus%20fax</BusinessFax><Mobile
xmlns="evententerprise">mobile</Mobile><Street
xmlns="evententerprise">street%20name</Street><City
xmlns="evententerprise">city</City><State
xmlns="evententerprise">state</State><PostalCode
xmlns="evententerprise">postal</PostalCode><Country
xmlns="evententerprise">country</Country></ROOT></submitContactResult></submitContactResponse></soap:Body></soap:Envelope>

If have also tried doing the following :

nsmgr.AddNamespace("xsd", "http://www.w3.org/2001/XMLSchema")
nsmgr.AddNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope/")

nsmgr.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance")

nsmgr.AddNamespace("", "evententerprise")


I am missing something but just can't figure it the hell out!!!
 
D

Dan Rogers

Hi Keith,

Remember, that everything outside of the Body element in the soap envelope
will not be passed to the method (this is why you get an XMLNode and not an
XML document). You shouldn't have to do a loadXMl at all, since the
serialization to an XmlNode is already finished at this point.

Here are some questions:
1. Is the XmlNode member named Data non-null when the method is invoked?
2. If it is non-null, it is an XMlNode and you can use it as an object
reference. The next question is what line of code is throwing the
uninitialized object exception? I suspect it is when you try to use the
node reference that can be returned by selectSingleNode. Remember, if
SelectSingleNode doesn't find anything, it returns null.

Adding namespaces won't do much for you since this isn't a namespace issue.
From the data you see coming back from your <ROOT> experiment, it seems
that the original request data is making it into your method just fine.
The non-namespace on your concatenated root node is what is making all of
those added child namespace markup appear - so again, the serializer seems
to be doing it's job correctly.

What is left to conclude then is that the Xpath expression is saying that
no results are found. You need to accommodate namespace in your xpath
queries - so I suspect your test is looking for Test in the non-namespace,
when your data is namespaced.

I hope this helps

Dan Rogers
Microsoft Corporation
--------------------
From: "Keith Chadwick" <[email protected]>
References: <#[email protected]>
Subject: Re: PLEASE HELP - object reference not set to instance of an object
Date: Mon, 29 Nov 2004 16:31:04 -0500
Lines: 70
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
X-RFC2646: Format=Flowed; Original
Message-ID: <[email protected]>
Newsgroups: microsoft.public.dotnet.framework.aspnet.webservices
NNTP-Posting-Host: ottawa-hs-64-26-156-220.s-ip.magma.ca 64.26.156.220
Path: cpmsftngxa10.phx.gbl!TK2MSFTNGXS01.phx.gbl!cpmsftngxa06.phx.gbl!TK2MSFTNGP08
.phx.gbl!TK2MSFTNGP15.phx.gbl
Xref: cpmsftngxa10.phx.gbl microsoft.public.dotnet.framework.aspnet.webservices:26878
X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webservices

Been trying everything Dan with no luck.

Have done the following:

Public Function submitContact(ByVal data As XmlNode) As xmldocument
Dim result As String
Dim resultDoc as new XmlDocument

resultDoc.loadxml("<root>" & data.innerXML() & "</root>")

return resultDoc
End

The xml comes back fine the same way I sent it down!!!

But if I do anything like
myElement=resultDoc.selectSingleNode("ROOT/contact/Title") it allways
allways no matter what comes back with the same error that I did not create
a new instance when I try to get at the element.

I tried a test=resultDoc.selectNodes("ROOT/contact/Title").count and it
always comes back as 0 for count. I am missing something obvious here.

In my example above the XML document that is returned to is below:

<?xml version="1.0" encoding="utf-8"?><soap:Envelope
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><submitContactRespo nse
xmlns="evententerprise"><submitContactResult><ROOT xmlns=""><Title
xmlns="evententerprise">Mr</Title><GivenName
xmlns="evententerprise">Andi</GivenName><MiddleName
xmlns="evententerprise">Thornton</MiddleName><Surname
xmlns="evententerprise">Cooper</Surname><Department
xmlns="evententerprise">department</Department><Assistant
xmlns="evententerprise">Assistants%20Name</Assistant><Manager
xmlns="evententerprise">Managers%20Name</Manager><Profession
xmlns="evententerprise">profession</Profession><JobTitle
xmlns="evententerprise">Job%20Title</JobTitle><EmployeeNumber
xmlns="evententerprise">employee%20number</EmployeeNumber><Company
xmlns="evententerprise">Company</Company><Language
xmlns="evententerprise">English</Language><email1
xmlns="evententerprise">[email protected]</email1><email2
xmlns="evententerprise">[email protected]</email2><email3
xmlns="evententerprise">[email protected]</email3><email
xmlns="evententerprise">email1</email><webpage
xmlns="evententerprise">http://web page address</webpage><BusinessPhone
 
K

Keith Chadwick

Yes I came to the exact same conclusion Dan, my problem was the namespace.
In my SOAP envelop it indicates xmlns="evententerprise" on the method
element. This xmlns is then being passed down to the child contact item so
my resulting xml in the node is:

<contact address="asdfasdfasdf" xmlns="evententerprise">
<Title>something</Title>
...
</contact>

You are correct when I do myelement=nodeset.selectSingleNode("//Title") then
try an reference myelement I get the object is not an instance because the
original select single node did not return a node.

So three points.
1) I am trying to get around even using namespaces as they do not provide me
anything beneficial at this point, but this concerns as they may in the
future.
2) Why does the Soap packet pass the xmlns namespace down to the root node
of the data variable that is passed and can I stop it from doing this?
3) Deal with the namespace issue and figure out how to work with it :)

The learning curve goes on!

Cheers
Keith
 
D

Dan Rogers

Hi Keith,

Namespace is an intrinsic part of XML. You could opt (for expedience sake)
to place the XML nodes in the non-namespace by passing an empty string when
you do the first CreateNode() via the DOM (assuming this is the case). But
the easiest thing to do, if you ask me, is to get good at dealing with
namespace now if you continue to deal with raw XML. Namespace adds the
added contextual information that lets someone know they have YOUR data
type and intended definitions, instead of relying on the name of the
element alone. Try to not think of the XML on the wire as a string that
you are manipulating - it's more than that and just looks like strings.
All modern (and well done) XML processors intrinsically differentiate
namespace, as do all well done XSLT and XPath processors. I think you'll
find an overload on selectSingleNode and SelectNodes that let you specify
what namespace you wish to find.

The answer to "Preventing your elements from being expressed in any given
namespace" requires that I know how exactly you are creating your XML
nodes. Myself, I always just deal with strong typed classes and use the
XmlRootAttribute, XmlElementAttribute and XmlAttributeAttribute to do fine
control over what specific atom names and namespaces appear on the wire,
and have not had to hand sling DOM code since the BizTalk Jumpstart Kit
tools back in 99.

Dan

--------------------
From: "Keith Chadwick" <[email protected]>
References: <#[email protected]>
<[email protected]>
 
K

Keith Chadwick

Hey Dan,

Yes I discovered that namespace was an intrinsic part of at least
webservices and soap when I tried to cheat and get around it, thats how one
learns.

I have managed to get everything going and like anything else it is just a
learning curve. Been using MSXML for years now so getting my head out of
that and into .NET Xml objects is a bit of strain. What makes it harder is
that my client side stuff is still using MSXML so jumping back and forth
realy starts to wear the cranium down. Same sort of situation as when you
spend all day writing JScript then go and write some VB.NET and keep
forgetting that there is a then on the if, you start to get a little
aggravated.

I managed to work with the namespaces after finding an example that actualy
stated that when you do AddNamespace on the namespaceManager that your xml
does not have to have the namespace already declared on the element just the
xmlns declared at either the element or parent node level. It seems obvious
now but when you do not know, it seems completely backwards when you think
about it. It's similar to doing a modify table script to add a column in
sql but selecting from the column before you ran the script, if you know
what I mean.

I have taken on a herculean product development from home and decided to do
it in .NET after spending 7 years doing ASP and SQL. You have to learn the
new technology at some point given the job market. Guess my old brain just
doesn't learn as quick as it did when I was a young sprig :)

Thanks for you help Dan, really appreciated.

Cheers
Keith
 
K

Keith Chadwick

The brains been working on namespace so I have a question .

On of the key things I would like to make available through this system is
for third party developers to use the webservices to retrieve and send
information back and forth over the wire. Basically we would act as a place
to store there data.

To do this there should be a schema available that they can point to in
order to understand the information they are receiving. For instance

<?xml?>
<root xmlns:eve="http://www.mydomain.com/schema">
<eve:something>
....
</eve:something>
</root>

When the above xml is parsed the actual schema is downloaded and used by the
parser to understand the xml. Am I right so far?

Now with regards to web services should the web service point to the same
schema as in

<system.web.services.webserver(Namespace:="http://www.mydomain.com/schema">

or is this something entirely different. I am not sure how the two would
link together?

You will have to forgive my naiveté but I have been avoiding schemas like
the plague for two years now :)

Cheers
Keith
 
D

Dan Rogers

Hi Kieth,

Great. Yes, there is a learning curve. There are some short-cuts though -
and giving up half of the hand coding was a blessing for me. If you
already have a schema for your on the wire body-payloads, try running them
through XsdObjectGen.exe (download it from Microsoft.Com/Downloads -
keyword is XsdObjectGen.exe) - it'll build most of the data management
classes for you automatically, so you don't have to have as-long a learning
curve. We built it to help me code this same kind of app faster and more
reliably.

Glad to help where I can

Dan
--------------------
From: "Keith Chadwick" <[email protected]>
References: <#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
 
D

Dan Rogers

Hi Kieth,

Actually, there is a very formal schema definition language at this point,
and it is an integral part of the web service programming stack. Once you
have built an ASP.net web service that is returning full fledged types (and
not XmlNodes), you will be able to see the schema and WSDL (web service
description language) standard format (kind of like IDL for web services)
by typing http://myservicehost/myserviceproject/myservice.aspx?WSDL in your
browser command line.

This is what is used by the add-web-reference proxy generation tool in
VS.net (which in turn calls into WSDL.exe to generate the code for the
proxy).

That WSDL will contain the schemas required, or references to the schemas
if you get into the more complex, well versioned cases at a later date. If
you start out with classes that are just plain classes you design and code,
you will get your schemas for free. An easy learning curve!

Hope this helps

Dan Rogers
Microsoft Corporation
--------------------
From: "Keith Chadwick" <[email protected]>
References: <#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
 
K

Keith Chadwick

Thanks Dan, the light buld just go a lot brighter. Of course if I type my
services instead of sending a xml 'blob' down to one service then the WDSL
of the service already acts as a schema to fully describe it. Now things
are becomming a lot clearer.

I try to leverage xml and schemas client side in order to prevent
submissions of data that is invalid. In many cases I do the same validation
again server side. So I guess I can simply use the WDSL of the service.

Thanks very much starting to see the bigger picture now and how things
integrate. Think I was still thinking to much along the lines of old asp
where we would leveral xml to do this stuff for us. Always got nervous when
the manufacturer said they did it for me, always ended up painting myself
into a corner :)

Cheers
Keith
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,705
Latest member
Stefkari24

Latest Threads

Top