title: SOAP C# Server with Java client null return
body: <p>I would like to check SOAP interoperability between distinct platforms; I am a Java programmer and do not know C# in particular.</p>
  
  <p>What have I managed to do in C#:</p>
  
  <p><strong>The transfer Object</strong>:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Xml.Serialization;
  
  [XmlRoot]
  public class Pack
  {
      private int id;
      private string name;
  
      public Pack()
      {   
      }
  
      public Pack(int anId, string aName)
      {
          this.id = anId;
          this.name = aName;
      }
  
      public void setId(int anId)
      {
          this.id = anId;
      }
  
      public int getId()
      {
          return id;
      }
  
      public void setName(string aName)
      {
          this.name = aName;
      }
  
      public string getName()
      {
          return name;
      }
  }
  </code></pre>
  
  <p><strong>The Web Service</strong>:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.Services;
  using System.Xml.Serialization;
  
  [WebService(Namespace = "http://tempuri.org/")]
  [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
  public class PackWS : System.Web.Services.WebService
  {
      private Dictionary&lt;int, Pack&gt; dictionary;
  
      public PackWS()
      {
          dictionary = new Dictionary&lt;int, Pack&gt;();
      }
  
      [WebMethod]
      [XmlInclude(typeof(String))]
      public String getName()
      {
          return "Goodbye world!";
      }
  
      [WebMethod]
      [XmlInclude(typeof(Pack))]
      public void addPackage(Pack package)
      {
         dictionary.Add(package.getId(), package);
      }
  
      [WebMethod]
      [XmlInclude(typeof(Pack))]
      public Pack getPackage(int id)
      {
          return dictionary[id];
      }
  }
  </code></pre>
  
  <p>The project builds and publishes the wsdl at the specified URI. I have added a Java client part:</p>
  
  <p><strong>The Transfer Object</strong>:</p>
  
  <pre><code>package user;
  
  import javax.xml.bind.annotation.XmlRootElement;
  
  @XmlRootElement
  public class Pack {
      private int id;
      private String name;
  
      public int getId() {
          return id;
      }
  
      public void setId(int id) {
          this.id = id;
      }
  
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  }
  </code></pre>
  
  <p><strong>The Interface:</strong></p>
  
  <pre><code>package user;
  
  import javax.jws.WebService;
  import javax.jws.soap.SOAPBinding;
  
  @WebService(targetNamespace = "http://tempuri.org/",
          portName = "PackWSSoap",
          wsdlLocation = "http://localhost:57265/Service.asmx?wsdl",
          serviceName = "PackWS")
  @SOAPBinding(style = SOAPBinding.Style.RPC)
  public interface PackWSSoap {
      void addPackage(Pack pack);
      Pack getPackage(int id);
      String getName();
  }
  </code></pre>
  
  <p><strong>The Service</strong></p>
  
  <pre><code>package user;
  
  import javax.xml.namespace.QName;
  import javax.xml.ws.Service;
  import java.net.MalformedURLException;
  import java.net.URL;
  
  public class PackageClient {
      public static void main(String[] args) {
          try {
              URL url = new URL("http://localhost:57265/Service.asmx?wsdl");
              QName qName = new QName("http://tempuri.org/", "PackWS");
  
              Service service = Service.create(url, qName);
  
              PackWSSoap packWS = service.getPort(PackWSSoap.class);
  
              System.out.println(packWS.getName());//null here
  
              packWS.addPackage(getPack());//C# exception here
  
              System.out.println(packWS.getPackage(getPack().getId()));//unreachable 
  
  
          } catch (MalformedURLException e) {
              e.printStackTrace();
          }
      }
  
      static Pack getPack() {
          return new Pack(){{
              setId(2);
              setName("name");
          }
          };
      }
  }
  </code></pre>
  
  <p>When calling the <code>getName()</code> method, it should return a simple String, but it returns null. When trying to add a package by calling <code>addPackage(pack)</code> I receive an exception from C# on the client, in Java: </p>
  
  <pre><code>Exception in thread "main" javax.xml.ws.soap.SOAPFaultException: System.Web.Services.Protocols.SoapException: Server was unable to process request. ---&gt; System.NullReferenceException: Object reference not set to an instance of an object.
     at PackWS.addPackage(Pack package) in f:\DS Projects\ds5\NET\App_Code\PackWS.cs:line 38
     --- End of inner exception stack trace ---
      at com.sun.xml.ws.fault.SOAP11Fault.getProtocolException(SOAP11Fault.java:163)
      at com.sun.xml.ws.fault.SOAPFaultBuilder.createException(SOAPFaultBuilder.java:94)
      at com.sun.xml.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:236)
      at com.sun.xml.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:206)
      at com.sun.xml.ws.client.sei.SEIStub.invoke(SEIStub.java:103)
      at $Proxy18.addPackage(Unknown Source)
      at user.PackageClient.main(PackageClient.java:25)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:597)
      at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
  </code></pre>
  
  <p>I do not believe to be anything wrong with the communication since the exception is passed well; but I believe I did not add something to the .NET server. Any help would be appreciated and sorry for the long post.</p>
  
  <p>Here is the <strong>WSDL</strong>:</p>
  
  <pre><code>&lt;wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:tns="http://tempuri.org/" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" targetNamespace="http://tempuri.org/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"&gt;
  &lt;wsdl:types&gt;
  &lt;s:schema elementFormDefault="qualified" targetNamespace="http://tempuri.org/"&gt;
  &lt;s:element name="getName"&gt;
  &lt;s:complexType/&gt;
  &lt;/s:element&gt;
  &lt;s:element name="getNameResponse"&gt;
  &lt;s:complexType&gt;
  &lt;s:sequence&gt;
  &lt;s:element minOccurs="0" maxOccurs="1" name="getNameResult" type="s:string"/&gt;
  &lt;/s:sequence&gt;
  &lt;/s:complexType&gt;
  &lt;/s:element&gt;
  &lt;s:element name="addPackage"&gt;
  &lt;s:complexType&gt;
  &lt;s:sequence&gt;
  &lt;s:element minOccurs="1" maxOccurs="1" name="package" nillable="true" type="tns:Pack"/&gt;
  &lt;/s:sequence&gt;
  &lt;/s:complexType&gt;
  &lt;/s:element&gt;
  &lt;s:complexType name="Pack"/&gt;
  &lt;s:element name="addPackageResponse"&gt;
  &lt;s:complexType/&gt;
  &lt;/s:element&gt;
  &lt;s:element name="getPackage"&gt;
  &lt;s:complexType&gt;
  &lt;s:sequence&gt;
  &lt;s:element minOccurs="1" maxOccurs="1" name="id" type="s:int"/&gt;
  &lt;/s:sequence&gt;
  &lt;/s:complexType&gt;
  &lt;/s:element&gt;
  &lt;s:element name="getPackageResponse"&gt;
  &lt;s:complexType&gt;
  &lt;s:sequence&gt;
  &lt;s:element minOccurs="1" maxOccurs="1" name="getPackageResult" nillable="true" type="tns:Pack"/&gt;
  &lt;/s:sequence&gt;
  &lt;/s:complexType&gt;
  &lt;/s:element&gt;
  &lt;/s:schema&gt;
  &lt;/wsdl:types&gt;
  &lt;wsdl:message name="getNameSoapIn"&gt;
  &lt;wsdl:part name="parameters" element="tns:getName"/&gt;
  &lt;/wsdl:message&gt;
  &lt;wsdl:message name="getNameSoapOut"&gt;
  &lt;wsdl:part name="parameters" element="tns:getNameResponse"/&gt;
  &lt;/wsdl:message&gt;
  &lt;wsdl:message name="addPackageSoapIn"&gt;
  &lt;wsdl:part name="parameters" element="tns:addPackage"/&gt;
  &lt;/wsdl:message&gt;
  &lt;wsdl:message name="addPackageSoapOut"&gt;
  &lt;wsdl:part name="parameters" element="tns:addPackageResponse"/&gt;
  &lt;/wsdl:message&gt;
  &lt;wsdl:message name="getPackageSoapIn"&gt;
  &lt;wsdl:part name="parameters" element="tns:getPackage"/&gt;
  &lt;/wsdl:message&gt;
  &lt;wsdl:message name="getPackageSoapOut"&gt;
  &lt;wsdl:part name="parameters" element="tns:getPackageResponse"/&gt;
  &lt;/wsdl:message&gt;
  &lt;wsdl:portType name="PackWSSoap"&gt;
  &lt;wsdl:operation name="getName"&gt;
  &lt;wsdl:input message="tns:getNameSoapIn"/&gt;
  &lt;wsdl:output message="tns:getNameSoapOut"/&gt;
  &lt;/wsdl:operation&gt;
  &lt;wsdl:operation name="addPackage"&gt;
  &lt;wsdl:input message="tns:addPackageSoapIn"/&gt;
  &lt;wsdl:output message="tns:addPackageSoapOut"/&gt;
  &lt;/wsdl:operation&gt;
  &lt;wsdl:operation name="getPackage"&gt;
  &lt;wsdl:input message="tns:getPackageSoapIn"/&gt;
  &lt;wsdl:output message="tns:getPackageSoapOut"/&gt;
  &lt;/wsdl:operation&gt;
  &lt;/wsdl:portType&gt;
  &lt;wsdl:binding name="PackWSSoap" type="tns:PackWSSoap"&gt;
  &lt;soap:binding transport="http://schemas.xmlsoap.org/soap/http"/&gt;
  &lt;wsdl:operation name="getName"&gt;
  &lt;soap:operation soapAction="http://tempuri.org/getName" style="document"/&gt;
  &lt;wsdl:input&gt;
  &lt;soap:body use="literal"/&gt;
  &lt;/wsdl:input&gt;
  &lt;wsdl:output&gt;
  &lt;soap:body use="literal"/&gt;
  &lt;/wsdl:output&gt;
  &lt;/wsdl:operation&gt;
  &lt;wsdl:operation name="addPackage"&gt;
  &lt;soap:operation soapAction="http://tempuri.org/addPackage" style="document"/&gt;
  &lt;wsdl:input&gt;
  &lt;soap:body use="literal"/&gt;
  &lt;/wsdl:input&gt;
  &lt;wsdl:output&gt;
  &lt;soap:body use="literal"/&gt;
  &lt;/wsdl:output&gt;
  &lt;/wsdl:operation&gt;
  &lt;wsdl:operation name="getPackage"&gt;
  &lt;soap:operation soapAction="http://tempuri.org/getPackage" style="document"/&gt;
  &lt;wsdl:input&gt;
  &lt;soap:body use="literal"/&gt;
  &lt;/wsdl:input&gt;
  &lt;wsdl:output&gt;
  &lt;soap:body use="literal"/&gt;
  &lt;/wsdl:output&gt;
  &lt;/wsdl:operation&gt;
  &lt;/wsdl:binding&gt;
  &lt;wsdl:binding name="PackWSSoap12" type="tns:PackWSSoap"&gt;
  &lt;soap12:binding transport="http://schemas.xmlsoap.org/soap/http"/&gt;
  &lt;wsdl:operation name="getName"&gt;
  &lt;soap12:operation soapAction="http://tempuri.org/getName" style="document"/&gt;
  &lt;wsdl:input&gt;
  &lt;soap12:body use="literal"/&gt;
  &lt;/wsdl:input&gt;
  &lt;wsdl:output&gt;
  &lt;soap12:body use="literal"/&gt;
  &lt;/wsdl:output&gt;
  &lt;/wsdl:operation&gt;
  &lt;wsdl:operation name="addPackage"&gt;
  &lt;soap12:operation soapAction="http://tempuri.org/addPackage" style="document"/&gt;
  &lt;wsdl:input&gt;
  &lt;soap12:body use="literal"/&gt;
  &lt;/wsdl:input&gt;
  &lt;wsdl:output&gt;
  &lt;soap12:body use="literal"/&gt;
  &lt;/wsdl:output&gt;
  &lt;/wsdl:operation&gt;
  &lt;wsdl:operation name="getPackage"&gt;
  &lt;soap12:operation soapAction="http://tempuri.org/getPackage" style="document"/&gt;
  &lt;wsdl:input&gt;
  &lt;soap12:body use="literal"/&gt;
  &lt;/wsdl:input&gt;
  &lt;wsdl:output&gt;
  &lt;soap12:body use="literal"/&gt;
  &lt;/wsdl:output&gt;
  &lt;/wsdl:operation&gt;
  &lt;/wsdl:binding&gt;
  &lt;wsdl:service name="PackWS"&gt;
  &lt;wsdl:port name="PackWSSoap" binding="tns:PackWSSoap"&gt;
  &lt;soap:address location="http://localhost:57265/Service.asmx"/&gt;
  &lt;/wsdl:port&gt;
  &lt;wsdl:port name="PackWSSoap12" binding="tns:PackWSSoap12"&gt;
  &lt;soap12:address location="http://localhost:57265/Service.asmx"/&gt;
  &lt;/wsdl:port&gt;
  &lt;/wsdl:service&gt;
  &lt;/wsdl:definitions&gt;
  </code></pre>