Herong's Tutorial Notes on Web Service and SOAP
Dr. Herong Yang, Version 4.00

Error: Object reference not set to an instance of an object

This section provides a tutorial example to try to use a SOAP 1.2 Web service with a service() call to the WSDL document. The test failed a System.NullReferenceException.

Base previous tests on my local system, I wrote this SOAP::Lite WSDL client program to try to use a SOAP 1.2 Web service:

#- GetSpeech_WSDL_1_2.pl
#- Copyright (c) 2009 by Dr. Herong Yang, herongyang.com
#- All rights reserved
#
   use SOAP::Lite +trace;

   my $wsdl = 'http://www.xmlme.com/WSShakespeare.asmx?WSDL';
   my ($phrase) = @ARGV;

#- Creating the SOAP client object
   my $client = SOAP::Lite->new()
      ->soapversion('1.2') # set to SOAP 1.2
      ->service($wsdl) # set the SOAPAction
      ->readable(true); # make the XML readable
      
#- Overriding the constant
   $SOAP::Constants::DEFAULT_HTTP_CONTENT_TYPE 
      = 'application/soap+xml';

#- Calling the Web service
   my $som = $client->GetSpeech($phrase);
   my $output = $som->result;
   print $output . "\n";

But the test result is not good:

C:\herong>GetSpeech_WSDL_1_2.pl "To be, or not to be"

POST http://www.xmlme.com/WSShakespeare.asmx HTTP/1.1
Accept: text/xml
Accept: multipart/*
Accept: application/soap
Content-Length: 831
Content-Type: application/soap+xml; charset=utf-8
SOAPAction: "http://xmlme.com/WebServices/GetSpeech"

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope
    xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/"
    xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/"
    xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/"
    soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding"
    xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
    xmlns:s="http://www.w3.org/2001/XMLSchema"
    xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
    xmlns:http="http://schemas.xmlsoap.org/wsdl/http/"
    xmlns:tns="http://xmlme.com/WebServices">
  <soap:Body>
    <tns:GetSpeech>
      <Request xsi:type="s:string">To be, or not to be</Request>
    </tns:GetSpeech>
  </soap:Body>
</soap:Envelope>
...
HTTP/1.1 500 Internal Server Error
Cache-Control: private
Connection: close
Date: ... 2009
Server: Microsoft-IIS/6.0
Content-Length: 690
Content-Type: application/soap+xml; charset=utf-8
Client-Response-Num: 1
X-AspNet-Version: 2.0.50727
X-Powered-By: ASP.NET

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Body>
    <soap:Fault>
      <soap:Code>
        <soap:Value>soap:Receiver</soap:Value>
      </soap:Code>
      <soap:Reason>
        <soap:Text xml:lang="en">
          System.Web.Services.Protocols.SoapException: 
            Server was unable to process request. ---> 
            System.NullReferenceException: 
            Object reference not set to an instance of an object.
            at WSShakespeare.Shakespeare.GetSpeech(String Request)
            --- End of inner exception stack trace ---
        </soap:Text>
      </soap:Reason>
      <soap:Detail />
    </soap:Fault>
  </soap:Body>
</soap:Envelope>

What's wrong here? My guess is that the parameter "Request" is under the default namespace, not is synch with the method "GetSpeech".

I cannot to fix this. May be you can.

Last update: 2009.

Sections in This Chapter

First WSDL Test Failed with SOAP::Lite

Downloading and Editing WSDL Document

Testing service() Function Locally

soapversion('1.2') Sets Correct Envelope namespace

Error: Object reference not set to an instance of an object

Dr. Herong Yang, updated in 2009
Error: Object reference not set to an instance of an object