-3

I am trying to save the below code and getting this compile error:

Non static method cannot be referenced from a static context: String System.HttpResponse.getStatus() at line

Specific error line:

if(httpResponse.getStatus() == XYZ_Constants.SUCCESS)

More complete code:

else if(sTerrName[0].Ter__r.Name!=null && !sTerrName[0].Ter__r.Name.equalsignorecase('DHE') && !sTerrName[0].Ter__r.Name.equalsignorecase('Global'))
{
    String sMId = SCode;
    HEP_Services__c serviceDetails = XYZ_Services__c.getInstance('Z1');
    if(serviceDetails!=null)
    {
        httpRequest.setEndpoint(serviceDetails.Endpoint_URL__c+serviceDetails.Service_URL__c+'?MID='+sMId);
        objTxnResponse.sRequest = httpRequest.toString();


        HTTPResponse httpResponse = http.send(httpRequest);    
        objTxnResponse.sResponse = httpResponse.getBody();     

        String Str = httpResponse.getBody().replaceAll('#','1');   
    }
}
//Gives the error on below line       
if(httpResponse.getStatus() == XYZ_Constants.SUCCESS)
{
    Wrapper = (OrderWrapper)JSON.deserialize(httpResponse.getBody(),OrderWrapper.class);
    objResponse.sStatus = XYZ_Constants.A_SUCCESS;
    objResponse.bRetry = false;

    if(poWrapper.errors != null){
        objTxnResponse.sStatus = XYZ_Constants.XYZ_FAILURE;
        objTxnResponse.bRetry = true;
    }       
}
  • 3
    I've formatted your provided code fragment to be more readable. You've been around long enough that I'd like to see more effort put into formatting your questions in the future. Also, giving the question a title and a code dump is generally not very helpful. Generally speaking, the more details you give, the better your chance of getting a good answer. – Derek F Mar 14 '18 at 20:30
2

You have a variable scope issue. It would have been more clear if you didn't introduce a naming collision by calling your HttpResponse instance httpResponse.

You can reproduce the issue below to see what I mean:

if (true)
{
    HttpResponse response = new HttpResponse();
}
system.debug(response.getStatus());

If you want to reference the response outside your else if block, you need to declare it before your entire if/else chain.

HttpResponse response;
if (/*some condition*/)
{
    // do some stuff
}
else if (/*other condition*/)
{
    // do some stuff
    response = new Http().send(someRequest);
    // do some stuff
}

if (response != null && response.getStatus() == 'some value)
{
    // do some stuff
}
  • 1
    Nice catch - I missed the scope error. – David Reed Mar 14 '18 at 20:35
  • 2
    @DavidReed If I hadn't formatted OP's code, I don't know if I would've caught the scope issue either (at least not as quickly. At any rate, still slower than "fast fingers" Adrian here). – Derek F Mar 14 '18 at 20:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.