-->

KSoap2 SoapObject Object Reference not set to an i

2019-01-28 05:25发布

问题:

I think this is a simple problem that others may be able to provide the missing link to. I have a Workflow wcf service that my .NET clients can communicate with properly. I have turned on tracing under the appfabric and can watch service calls execute properly so I feel comfortable the service is not the problem. My Android calls never hit the first trace point.

In Android using ksoap2 (2.6.5) when I call this line I get an Object reference not set error:

SoapObject response = (SoapObject)envelope.getResponse();

To me this seems simple the envelope must be null and when we try to access it to exec the getResponse we have our error and yet in the debugger it shows it as a valid object. Furthermore, I can't find the documentation to explain and every tutorial I have seen has it this way so....?

One thing that is different is that in .NET I would create an object pass the object letting .NET serialize it for me. Here I add several parameters and they are all strings but the enum I will show as it is a possible culprit. Here is my full Java implementation:

String NAMESPACE = "http://tempuri.org/";
String METHOD_NAME = "LiftDataExchange";
String SOAP_ACTION = "http://tempuri.org/ILiftDataExchange/ProcessDataRequest";
String URL = "http://www.icyarmtesting.com/LiftDataExchange.xamlx";
String SOAP_REMOTE_NAMESPACE = "http://schemas.datacontract.org/2004/07/IronMikeDataExchangeWorkflowService.Enum";

String X = "";
ExchangeEnumerations ExrcsEnum = ExchangeEnumerations.GetNextWorkout;

//Initialize soap request
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

//add parameters
PropertyInfo piAct = new PropertyInfo();
piAct.setName("Action");
piAct.setValue(ExrcsEnum);
piAct.setType(ExchangeEnumerations.class);
request.addProperty(piAct);

//Declare the version of the SOAP request
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.addMapping(SOAP_REMOTE_NAMESPACE, "ExchangeEnumerations", ExchangeEnumerations.class, ExchangeEnumerationsEnumClass.getInstance());
envelope.dotNet = true;
envelope.setOutputSoapObject(request);

//Needed to make the internet call
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.debug = true;
try 
    {          
  //this is the actual part that will call the webservice
  androidHttpTransport.call(SOAP_ACTION, envelope); 
  SoapObject response = (SoapObject)envelope.getResponse();
  X = response.getProperty(0).toString();
}
catch (Exception e)...... 

Here is my request dump:

<v:Envelope xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:d="http://www.w3.org/2001/XMLSchema" xmlns:c="http://schemas.xmlsoap.org/soap/encoding/" xmlns:v="http://schemas.xmlsoap.org/soap/envelope/">
    <v:Header />
    <v:Body>
        <LiftDataExchange xmlns="http://tempuri.org/" id="o0" c:root="1">
            <UserName i:type="d:string">STERILIZED_OUT</UserName>
            <Password i:type="d:string">STERILIZED_OUT</Password>
            <ExerciseProgramID i:type="d:string">STERILIZED_OUT</ExerciseProgramID>
            <IncomingWorkoutData i:type="d:string">STERILIZED_OUT</IncomingWorkoutData>
            <Action i:type="n0:ExchangeEnumerations" xmlns:n0="http://schemas.datacontract.org/2004/07/IronMikeDataExchangeWorkflowService.Enum">GetNextWorkout</Action>
        </LiftDataExchange>
    </v:Body>
</v:Envelope>

Here is my response dump:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
    <s:Body>
        <s:Fault>
            <faultcode xmlns:a="http://schemas.microsoft.com/net/2005/12/windowscommunicationfoundation/dispatcher">a:InternalServiceFault</faultcode>
            <faultstring xml:lang="en-US">Object reference not set to an instance of an object.</faultstring>
            <detail>
                <ExceptionDetail xmlns="http://schemas.datacontract.org/2004/07/System.ServiceModel" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
                <HelpLink i:nil="true"/>
                <InnerException i:nil="true"/>
                <Message>Object reference not set to an instance of an object.</Message>                
                <StackTrace>   at System.ServiceModel.Activities.WorkflowOperationContext.HandleEndResumeBookmark(IAsyncResult result)&#xD; at System.ServiceModel.Activities.WorkflowOperationContext.OnResumeBookmark()&#xD; at System.ServiceModel.Activities.WorkflowOperationContext..ctor(Object[] inputs, OperationContext operationContext, String operationName, Boolean performanceCountersEnabled, Boolean propagateActivity, Transaction currentTransaction, WorkflowServiceInstance workflowInstance, IInvokeReceivedNotification notification, WorkflowOperationBehavior behavior, ServiceEndpoint endpoint, TimeSpan timeout, AsyncCallback callback, Object state)&#xD; at System.ServiceModel.Activities.Description.WorkflowOperationBehavior.WorkflowOperationInvoker.OnBeginServiceOperation(WorkflowServiceInstance workflowInstance, OperationContext operationContext, Object[] inputs, Transaction currentTransaction, IInvokeReceivedNotification notification, TimeSpan timeout, AsyncCallback callback, Object state)&#xD; at System.ServiceModel.Activities.Dispatcher.ControlOperationInvoker.ControlOperationAsyncResult.PerformOperation()&#xD; at System.ServiceModel.Activities.Dispatcher.ControlOperationInvoker.ControlOperationAsyncResult.HandleEndGetInstance(IAsyncResult result)&#xD; at System.ServiceModel.Activities.Dispatcher.ControlOperationInvoker.ControlOperationAsyncResult.Process()&#xD; at System.ServiceModel.Activities.Dispatcher.ControlOperationInvoker.ControlOperationAsyncResult..ctor(ControlOperationInvoker invoker, Object[] inputs, IInvokeReceivedNotification notification, TimeSpan timeout, AsyncCallback callback, Object state)&#xD; at System.ServiceModel.Activities.Dispatcher.ControlOperationInvoker.InvokeBegin(Object instance, Object[] inputs, IInvokeReceivedNotification notification, AsyncCallback callback, Object state)&#xD; at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc&amp; rpc)&#xD; at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc&amp; rpc)&#xD; at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc&amp; rpc)&#xD; at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)</StackTrace><Type>System.NullReferenceException</Type></ExceptionDetail></detail></s:Fault></s:Body></s:Envelope>

UPDATE:

Switching to Edwin's method calls had the same result. So I upgraded from KSoap2 2.6.5 3.0.0-RC4 and now I am getting a NullRefException. With the prior version I would hit the service and throw the object not set error. With this version I do not hit the service and throw NullRef. The code is the same the only thing that changes is which version of KSoap I use. I wrote a simple service (remember these are Workflow Services) that takes a string in, changes it, and kicks a string back out.

FWIW here is my code to make the call:

String SOAP_ACTION = "http://tempuri.org/";
    String OPERATION_NAME="ChangeName";
    final String WSDL_TARGET_NAMESPACE = SOAP_ACTION;
    final String SOAP_ADDRESS= "http://www.icyarmtesting.com/ServiceTest/Service1.xamlx";
    Exception exception;
    String ErrorMsg="";
    String TEST_URL="" ;

    SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE, OPERATION_NAME);

    //PropertyInfo piName = new PropertyInfo();
    //piName.setName("UserName");
    //piName.setValue("ramjet");
    //piName.setType(String.class);
    //request.addProperty(piName);
    request.addProperty("UserName", "ramjet");

    SoapSerializationEnvelope  envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);
    envelope.dotNet= true;
    envelope.setOutputSoapObject(request);

    HttpTransportSE httpTransport =null;
    if(!TEST_URL.equals(""))
        httpTransport = new HttpTransportSE(TEST_URL);
    else
        httpTransport = new HttpTransportSE(SOAP_ADDRESS);



    Object Response =null;
     try
     {
        httpTransport.call(SOAP_ACTION + "IService/" + OPERATION_NAME, envelope);
        Response = envelope.getResponse();
      }
     catch (SocketTimeoutException ex) 
     {
         ErrorMsg="Unable to connect";
          Response=null;
         exception=ex;
      }
      catch (IOException ie) 
      {
       ErrorMsg="Unable to connect";
      Response=null;
       exception=ie;
      }
       catch (Exception e) 
       {
        ErrorMsg="Unable to connect";
        Response=null;
        exception=e;
     }

回答1:

Change the following:

SoapObject response = (SoapObject)envelope.getResponse();
  X = response.getProperty(0).toString();

to be:

SoapObject response = (SoapObject)envelope.bodyIn;
                if(response != null)
                {
                      X=response.getProperty(0).toString();
                }


回答2:

This was the class that i am using for calling web service hope this might help you

public class CallSOAP {

private String SOAP_ACTION="your action url";
private String OPERATION_NAME="";//The web method OR web service you are going to call
private final String WSDL_TARGET_NAMESPACE=SOAP_ACTION;
private  final String SOAP_ADDRESS=Configuration.WEB_SERVICE_URL;//ip-address of serever where you host your service 
private Exception exception;
private String ErrorMsg="";
private String TERST_URL="" ;
public Object call(String MethodeName,ArrayList<PropertyInfo> Parameters){
  this.OPERATION_NAME=MethodeName;
  SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE, OPERATION_NAME);
 if(Parameters != null && Parameters.size()>0){
   for(PropertyInfo propertyInfo : Parameters){
      request.addProperty(propertyInfo);
 }
}

SoapSerializationEnvelope  envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet= true;
envelope.setOutputSoapObject(request);
HttpTransportSE httpTransport =null;
if(!this.TERST_URL.equals(""))
  httpTransport = new HttpTransportSE(this.TERST_URL);
else
  httpTransport = new HttpTransportSE(SOAP_ADDRESS);
 Object Response =null;
 try{
    httpTransport.call(SOAP_ACTION+OPERATION_NAME, envelope);
    Response=envelope.getResponse();
  }
 catch (SocketTimeoutException ex) {
     this.ErrorMsg="Unable to connect";
      Response=null;
     this.exception=ex;
  }
  catch (IOException ie) {
   this.ErrorMsg="Unable to connect";
  Response=null;
   this.exception=ie;
  }
   catch (Exception e) {
    this.ErrorMsg="Unable to connect";
    Response=null;
    this.exception=e;
 }
      return Response;
   }
}


回答3:

I know its too late to answer this but I faced the same problem and the problem in my code was that I typed the property.setname() incorrect .. make sure you wrote the correct name of the method parameter (copy past it from the wsdl) I'm using ksoap2 3.1.1 btw!



回答4:

I am using below function in my class to work with SOAP Web-Service.

public static String DeleteGame(String GameIDValue, String PlayerIdValue) {

        String responce = null; 

        SoapObject request = new SoapObject(SOAP_NAMESPACE, SOAP_METHOD_DeleteGame);

        PropertyInfo GameId = new PropertyInfo();
        PropertyInfo PlayerId = new PropertyInfo();

        GameId.setName("gameid");
        GameId.setValue(GameIDValue);

        PlayerId.setName("PlayerID");
        PlayerId.setValue(PlayerIdValue);

        request.addProperty(GameId);
        request.addProperty(PlayerId);

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(request);
        HttpTransportSE aht = new HttpTransportSE(SOAP_URL);
        try {
            aht.call(SOAP_ACTION_DeleteGame, envelope);
            SoapPrimitive LoginResult;
            LoginResult = (SoapPrimitive)envelope.getResponse();
            System.out.println("=================Delete Game Results: "+LoginResult.toString());
            //System.out.println(LoginResult.toString());
            responce = LoginResult.toString();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        }

        return responce;
    }