I am attempting to test the following callout method:

  public with sharing class MMarkAPI {

      /******  CALLOUT  ******/
      public static HttpResponse MMarkAPIcallout(String domain){
        string endpoint = 'https://someapi.com/api/v0/company/mm/details?company_id=';

        //get companyID using domain (this is a separate callout)
        string companyID = companyIDFetcher.companyIDFetcher(domain);

        //system.debug('companyID is' +companyID);
        httpRequest req = new httpRequest();


//set method as containing the endpoint + companyID

//set timeout

        httpResponse res = new http().send(req);

        return res;

And here's the test:

private class MMarkAPITest {

    static testMethod void MMarkAPIUnitTest() {
        test.setMock(HttpCalloutMock.class, new MMarkAPITestHttpCalloutMock());

        // Call method to test.
        // This causes a fake response to be sent
        // from the class that implements HttpCalloutMock.



However, when I run this test from the classes menu in 'Setup' I get the following error and no test coverage:

system.nullpointerexception attempt to dereference a null object salesforce

Updating with the detailed description of the error message that occurs when running the test:

    Time Started    6/9/2017 8:17 AM
Class   MMarkAPITest
Method Name MMarkAPIUnitTest
Pass/Fail   Fail
Error Message   System.NullPointerException: Attempt to de-reference a null object
Stack Trace Class.MMarkAPITestHttpCalloutMock.respond: line 12, column 1
Class.companyIDFetcher.companyIDFetcher: line 12, column 1
Class.MMarkAPI.MMarkAPIcallout: line 8, column 1
Class.MMarkAPITest.MMarkAPIUnitTest: line 11, column 1

Also including the mock callout class:

global with sharing class MMarkAPITestHttpCalloutMock implements HttpCalloutMock {

//A single method that acts as a webservice to simulate 3rd party services
global HTTPResponse respond(HTTPRequest req){
    //Look at the httpRequest that was sent via Apex to possibly determine how we will respond

    //Construct the object that we want to respond with
    MMarkAPI.JSON2Apex response = new MMarkAPI.JSON2Apex();
    response.data.name = 'name'; //this is line 12
    response.data.description = 'description';
    response.data.website = 'website';
    response.data.mattermark_score = 'mattermark_score';
    response.data.employees = 'employees';
    response.data.employees_month_ago = 'employees_month_ago';
    response.data.employees_6_months_ago = 'employees_6_months_ago';
    response.data.website_uniques = 'website_uniques';
    response.data.mobile_downloads = 'mobile_downloads';
    response.data.stage = 'stage';
    response.data.total_funding = 'total_funding';
    response.data.last_funding_amount = 'last_funding_amount';
    response.data.last_funding_amount = 'last_funding_date';
    response.data.location = 'location';
    response.data.city = 'city';
    response.data.state = 'state';
    response.data.country = 'country';
    response.data.zip = 'zip';
    response.data.twitter_handle = 'twitter_handle';
    response.data.facebook_handle = 'facebook_handle';
    response.data.linkedin_id = 'linkedin_id';

    response.data.funding = new List<MMarkAPI.funding>{new MMarkAPI.funding()};
    response.data.funding[0].amount = 'amount';
    response.data.funding[0].deal_currency = 'deal_currency';
    response.data.funding[0].series = 'series';
    response.data.funding[0].funding_date = 'funding_date';
    response.data.funding[0].news_url = 'news_url';
    response.data.funding[0].source = 'source';
    response.data.funding[0].investor_ids = new List<Integer>{1};
    response.data.funding[0].investors  = 'investors';

    response.data.stories = new List<MMarkAPI.stories>{new MMarkAPI.stories()};
    response.data.stories[0].title = 'title';
    response.data.stories[0].url = 'url';
    response.data.stories[0].story_date = 'story_date';
    response.data.stories[0].source = 'source';

    response.data.growth_scores = new List<MMarkAPI.growth_scores>{new MMarkAPI.growth_scores()};
    response.data.growth_scores[0].recorded_at = 'recorded_at';
    response.data.growth_scores[0].score = 'score';

    response.data.mindshare_scores = new List<MMarkAPI.growth_scores>{new MMarkAPI.growth_scores()};
    response.data.mindshare_scores[0].recorded_at = 'recorded_at';
    response.data.mindshare_scores[0].score = 'score';

    response.data.acquired_by = new List<MMarkAPI.Meta>{new MMarkAPI.Meta()};

    response.data.acquired_companies = new List<MMarkAPI.Meta>{new MMarkAPI.Meta()};

    response.data.est_founding_date = 'est_founding_date';

    response.data.business_models = new List<String>{'business_model'};

    response.data.industries = new List<String>{'industry'};

    response.data.mattermark_weekly_momentum = new List<MMarkAPI.growth_scores>{new MMarkAPI.growth_scores()};
    response.data.mattermark_weekly_momentum[0].recorded_at = 'recorded_at';
    response.data.mattermark_weekly_momentum[0].score = 'score';

    response.data.employee_count = new List<MMarkAPI.growth_scores>{new MMarkAPI.growth_scores()};
    response.data.employee_count[0].recorded_at = 'recorded_at';
    response.data.employee_count[0].score = 'score';

    response.data.alexa_rank = new List<MMarkAPI.growth_scores>{new MMarkAPI.growth_scores()};
    response.data.alexa_rank[0].recorded_at = 'recorded_at';
    response.data.alexa_rank[0].score = 'score';

    response.data.facebook_talking_count = new List<MMarkAPI.growth_scores>{new MMarkAPI.growth_scores()};
    response.data.facebook_talking_count[0].recorded_at = 'recorded_at';
    response.data.facebook_talking_count[0].score = 'score';

    response.data.facebook_like_count = new List<MMarkAPI.growth_scores>{new MMarkAPI.growth_scores()};
    response.data.facebook_like_count[0].recorded_at = 'recorded_at';
    response.data.facebook_like_count[0].score = 'score';

    response.data.twitter_follower_count = new List<MMarkAPI.growth_scores>{new MMarkAPI.growth_scores()};
    response.data.twitter_follower_count[0].recorded_at = 'recorded_at';
    response.data.twitter_follower_count[0].score = 'score';

    response.data.twitter_mentions = new List<MMarkAPI.Meta>{new MMarkAPI.Meta()};

    response.data.linkedin_follower_count = new List<MMarkAPI.growth_scores>{new MMarkAPI.growth_scores()};
    response.data.linkedin_follower_count[0].recorded_at = 'recorded_at';
    response.data.linkedin_follower_count[0].score = 'score';

    response.data.itunes_usa_weekly_download = new List<MMarkAPI.Meta>{new MMarkAPI.Meta()};

    response.data.inbound_links = new List<MMarkAPI.growth_scores>{new MMarkAPI.growth_scores()};
    response.data.inbound_links[0].recorded_at = 'recorded_at';
    response.data.inbound_links[0].score = 'score';

    response.data.est_monthly_uniques = new List<MMarkAPI.growth_scores>{new API.growth_scores()};
    response.data.est_monthly_uniques[0].recorded_at = 'recorded_at';
    response.data.est_monthly_uniques[0].score = 'score';

    response.data.added_date = 'added date';

    response.data.revenue_range = 'revenue_range';

    response.data.google_play_apps = new List<MMarkAPI.google_play_apps>{new MMarkAPI.Google_play_apps()};
    response.data.google_play_apps[0].id = 1;
    response.data.google_play_apps[0].app_name = 'app_name';
    response.data.google_play_apps[0].package_id = 'package_id';
    response.data.google_play_apps[0].company_id = 1;
    response.data.google_play_apps[0].company_name = 'company name';
    response.data.google_play_apps[0].company_domain = 'company domain';
    response.data.google_play_apps[0].play_url = 'play_url';

    response.data.itunes_apps = new List<API.Itunes_apps>{new MMarkAPI.Itunes_apps()};
    response.data.itunes_apps[0].developer_url = 'developer_url';
    response.data.itunes_apps[0].itunes_id = 'itunes_id';
    response.data.itunes_apps[0].itunes_url = 'itunes_url';
    response.data.itunes_apps[0].app_name = 'app_name';

    response.data.ipos = new List<MMarkAPI.Meta>{new MMarkAPI.Meta()};

    //Instantiate a new httpResponse
    httpResponse res = new httpResponse();

    //Set the Status

    //Set the StatusCode

    //Set the Body to the serialized form of the instance of the stackExchangeAPI.responseResource

    //Return the httpResponse
    return res;


And here's the (working) deserialisation file that the above class is based on:

      /******  JSON DESERIALISATION  ******/
  public class Meta {

  public class Google_play_apps {
    public Integer id;
    public String app_name;
    public String package_id;
    public Integer company_id;
    public String company_name;
    public String company_domain;
    public String play_url;

  public class JSON2Apex {
    public Meta meta;
    public Data data;

  public class Funding {
    public String amount;
    public String deal_currency;
    public String series;
    public String funding_date;
    public String news_url;
    public String source;
    public List<Integer> investor_ids;
    public String investors;

  public class Growth_scores {
    public String recorded_at;
    public String score;

  public class Data {
    public String name;
    public String description;
    public String website;
    public String mattermark_score;
    public String employees;
    public String employees_month_ago;
    public String employees_6_months_ago;
    public String website_uniques;
    public String mobile_downloads;
    public String stage;
    public String total_funding;
    public String last_funding_amount;
    public String last_funding_date;
    public String location;
    public String city;
    public String state;
    public String country;
    public String zip;
    public String twitter_handle;
    public String facebook_handle;
    public String linkedin_id;
    public List<Funding> funding;
    public List<Stories> stories;
    public List<Growth_scores> growth_scores;
    public List<Growth_scores> mindshare_scores;
    public List<Meta> acquired_by;
    public List<Meta> acquired_companies;
    public String est_founding_date;
    public List<String> business_models;
    public List<String> industries;
    public List<Growth_scores> mattermark_weekly_momentum;
    public List<Growth_scores> employee_count;
    public List<Growth_scores> alexa_rank;
    public List<Growth_scores> facebook_talking_count;
    public List<Growth_scores> facebook_like_count;
    public List<Growth_scores> twitter_follower_count;
    public List<Meta> twitter_mentions;
    public List<Growth_scores> linkedin_follower_count;
    public List<Meta> itunes_usa_weekly_download;
    public List<Growth_scores> inbound_links;
    public List<Growth_scores> est_monthly_uniques;
    public String added_date;
    public String revenue_range;
    public List<Google_play_apps> google_play_apps;
    public List<Itunes_apps> itunes_apps;
    public List<Meta> ipos;

  public class Stories {
    public String title;
    public String url;
    public String story_date;
    public String source;

  public class Itunes_apps {
    public String developer_url;
    public String itunes_id;
    public String itunes_url;
    public String app_name;
  • Which line throws the null pointer? You should have gotten a stack trace.
    – Adrian Larson
    Jun 8, 2017 at 16:58
  • Hi Adrian and thank you for your reply. When I am trying to run the code from the anonymous window I get this error (Btw, the above error persists even when I change the class to 'public'): Line: 19, Column: 1 Method is not visible: MMarkAPITest.MMarkAPIUnitTest() However, when I choose Run Test from the Setup menu (Setup -> Apex Classes --> MMarkAPITest --> Run Test I get this: system.nullpointerexception attempt to dereference a null object salesforce Jun 8, 2017 at 21:58
  • @KamilMieczakowski You will need the additional details that go with that NullPointerException to isolate the problem. The line number in particular. If you can indicate which line if your sample code corresponds to that line number you will be well on your way to solving the problem. Capturing the Debug log when the test is running would also be helpful. Jun 9, 2017 at 1:05
  • @Daniel Ballinger, now updated my post with the detailed error message. Thank you. Jun 9, 2017 at 7:30
  • @KamilMieczakowski The stack trace indicates the problem is on line 12 of the MMarkAPITestHttpCalloutMock class. Can you include your Mock code in the question please. Indicating which line 12 is would also be helpful. Jun 9, 2017 at 7:37

1 Answer 1


The response.data is null. Before you set the name on it you first assign the data to a Meta.Data instance.


//Construct the object that we want to respond with
MMarkAPI.JSON2Apex response = new MMarkAPI.JSON2Apex();

response.data = new MMarkAPI.Data();

response.data.name = 'name'; //this was line 12

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.