I am attempting to display an error message on a VF page if I get a time out error from an api call. I am using try catch, to get to the exception, but I would like to stop further code execution after.


        gmsSearchResults = GMSServiceAPI.doAdvancedSearch(cfg); //perform the call

     }catch (CalloutException e){

         if(e.getMessage() == 'Read timed out'){

                ApexPages.Message myMsg = new ApexPages.Message(ApexPages.severity.ERROR, ' We have a timeout Error. GMS did not return results for the allotted time.');

if(gmsSearchResults.size() > 49){
            ApexPages.Message myMsg = new ApexPages.Message(ApexPages.severity.INFO, ' Search returned more than 50 records. Please refine your search criteria.');

I am seeing the exception and the error in my debug log, but the rest of my code fails when it hits the if statement outside of the try/catch, with an error of "Attempt to de-reference a null object"

1 Answer 1


Simply add a return statement.

catch (CalloutException e)
    // handle error

If your method return type is PageReference rather than void, then change return to return null.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .