[SalesForce] wsdl2apex class and System.NullPointerException: Attempt to de-reference a null object

I am trying to connect to a weather API.

The class was created from wsdl2apex

public class AsyncWeatherRequestWebService2 {
public class GetCitiesByCountryResponse_elementFuture extends System.WebServiceCalloutFuture {
    public String getValue() {
        WeatherRequestWebService2.GetCitiesByCountryResponse_element response = (WeatherRequestWebService2.GetCitiesByCountryResponse_element)System.WebServiceCallout.endInvoke(this);
        return response.GetCitiesByCountryResult;
    }
}
public class GetWeatherResponse_elementFuture extends System.WebServiceCalloutFuture {
    public String getValue() {
        WeatherRequestWebService2.GetWeatherResponse_element response = (WeatherRequestWebService2.GetWeatherResponse_element)System.WebServiceCallout.endInvoke(this);
        return response.GetWeatherResult;
    }
}
public class AsyncGlobalWeatherSoap {
    public String endpoint_x = 'http://www.webservicex.net/globalweather.asmx';
    public Map<String,String> inputHttpHeaders_x;
    public String clientCertName_x;
    public Integer timeout_x;
    private String[] ns_map_type_info = new String[]{'http://www.webserviceX.NET', 'WeatherRequestWebService2'};
    public AsyncWeatherRequestWebService2.GetCitiesByCountryResponse_elementFuture beginGetCitiesByCountry(System.Continuation continuation,String CountryName) {
        WeatherRequestWebService2.GetCitiesByCountry_element request_x = new WeatherRequestWebService2.GetCitiesByCountry_element();
        request_x.CountryName = CountryName;
        return (AsyncWeatherRequestWebService2.GetCitiesByCountryResponse_elementFuture) System.WebServiceCallout.beginInvoke(
          this,
          request_x,
          AsyncWeatherRequestWebService2.GetCitiesByCountryResponse_elementFuture.class,
          continuation,
          new String[]{endpoint_x,
          'http://www.webserviceX.NET/GetCitiesByCountry',
          'http://www.webserviceX.NET',
          'GetCitiesByCountry',
          'http://www.webserviceX.NET',
          'GetCitiesByCountryResponse',
          'WeatherRequestWebService2.GetCitiesByCountryResponse_element'}
        );
    }
    public AsyncWeatherRequestWebService2.GetWeatherResponse_elementFuture beginGetWeather(System.Continuation continuation,String CityName,String CountryName) {
        WeatherRequestWebService2.GetWeather_element request_x = new WeatherRequestWebService2.GetWeather_element();
        request_x.CityName = CityName;
        request_x.CountryName = CountryName;
        return (AsyncWeatherRequestWebService2.GetWeatherResponse_elementFuture) System.WebServiceCallout.beginInvoke(
          this,
          request_x,
          AsyncWeatherRequestWebService2.GetWeatherResponse_elementFuture.class,
          continuation,
          new String[]{endpoint_x,
          'http://www.webserviceX.NET/GetWeather',
          'http://www.webserviceX.NET',
          'GetWeather',
          'http://www.webserviceX.NET',
          'GetWeatherResponse',
          'WeatherRequestWebService2.GetWeatherResponse_element'}
        );
    }
}

}

I am just testing this using the execute anonymous window

AsyncWeatherRequestWebService2.GetWeatherResponse_elementFuture GetWeatherRequestService;

 AsyncWeatherRequestWebService2.AsyncGlobalWeatherSoap GetWeatherRequest = new AsyncWeatherRequestWebService2.AsyncGlobalWeatherSoap();   

 Integer TIMEOUT_INT_SECS = 90;  
 Continuation cont = new Continuation(TIMEOUT_INT_SECS);
 cont.continuationMethod = 'processResponse';     


GetWeatherRequestService = GetWeatherRequest.beginGetWeather(cont, 'Paris','france'); 

GetWeatherRequestService.getValue(); 

and the error i am receiving is System.NullPointerException: Attempt to de-reference a null object pointing to return response.GetWeatherResult;

when i test the webservice using SOAPUI, it does return something.

so is this issue in the way i am calling the webservice? is the generated class missing something?

help please!


It is probably because of the
"cont.continuationMethod = 'processResponse'; "

does it require a call back method?

public Object processResponse() {
result = GetWeatherRequestService.getValue();
return result;

Best Answer

Wsdl2Apex now creates two sets of Apex classes for interacting with the SOAP based web service.

  • The first use WebServiceCallout.invoke and are synchronous. These are historically all that were generated.
  • The second use what is called a Continuation. These are new in Spring 15. See Make Long-Running Callouts from a Visualforce Page. It will use WebServiceCallout.beginInvoke and WebServiceCallout.endInvoke calls

For the purposes of testing the web service via anonymous apex you will want to use the former. It will have a name like WeatherRequestWebService2 based on your sample code.

E.g. (After adding Remote Sites setting for http://www.webservicex.net)

wwwWebservicexNet.GlobalWeatherSoap ws = new wwwWebservicexNet.GlobalWeatherSoap();

//System.debug(ws.GetCitiesByCountry('France'));

string weather = ws.GetWeather('Paris / Le Bourget', 'France');
System.debug(weather);

Gives:

<?xml version="1.0" encoding="utf-16"?>
<CurrentWeather>
  <Location>Paris / Le Bourget, France (LFPB) 48-58N 002-27E 65M</Location>
  <Time>Apr 28, 2015 - 07:00 PM EDT / 2015.04.28 2300 UTC</Time>
  <Wind> from the N (010 degrees) at 5 MPH (4 KT):0</Wind>
  <Visibility> greater than 7 mile(s):0</Visibility>
  <Temperature> 42 F (6 C)</Temperature>
  <DewPoint> 35 F (2 C)</DewPoint>
  <RelativeHumidity> 75%</RelativeHumidity>
  <Pressure> 30.15 in. Hg (1021 hPa)</Pressure>
  <Status>Success</Status>
</CurrentWeather>
Related Topic