1

I am getting error: Attempt to de-reference a null object Error is in expression '{!doFetchCOVIDInfo}' in component in page covid19page: Class.COVID19Manager.doFetchCOVIDInfo:

Aim: I am trying to fetch all the Covid19 cases via API (here) and display(after parsing the returned JSON) after a button click.

Following is the COVID19Manager Class:

public class COVID19Manager{

   public static list<WrapperCOVID19Info> covid19{get;set;}
   public COVID19Manager(){
       System.debug('###Constructor called!!!!!!!!');
       covid19 = new list<WrapperCOVID19Info>();
   }


    public class WrapperCOVID19Info{
        public string country{get;set;}
        public string country_abbreviation{get;set;}
        public string total_cases{get;set;}
        public string new_cases{get;set;}
        public string total_deaths{get;set;}
        public string new_deaths{get;set;}
        public string total_recovered{get;set;}
        public string active_cases{get;set;}
        public string serious_critical{get;set;}
        public string cases_per_mill_pop{get;set;}
        public string flag{get;set;}

    }

    public void doFetchCOVIDInfo(){


        Http h = new Http();
        HttpRequest req = new HttpRequest();
        HttpResponse res = new HttpResponse();
        req.setHeader('Content-Type','application/json');
        req.setMethod('GET');
        req.setEndpoint('https://corona-virus-stats.herokuapp.com/api/v1/cases/countries-search');
        res = h.send(req);
        JSONParser parser = JSON.createParser(res.getBody());

        while (parser.nextToken() != null){
             if (parser.getCurrentToken() == JSONToken.START_ARRAY) {
                 while (parser.nextToken() != null) {
                      if (parser.getCurrentToken() == JSONToken.START_OBJECT) {
                          WrapperCOVID19Info covidCase = new WrapperCOVID19Info();
                          covidCase = (WrapperCOVID19Info)parser.readValueAs(WrapperCOVID19Info.class);
                              if(string.isNotBlank(covidCase.Country)){
                              covid19.add(covidCase);
                              }
                      }
                 }   
             }
         }

      system.debug(covid19.size());  
    }//END of method

}//END of Class

This list is then displayed using vf page below:

<apex:page Controller="COVID19Manager" >
<apex:form >
<apex:commandButton value="Get List" action="{!doFetchCOVIDInfo}"/>

     <apex:pageBlock title="Covid19List">
             <apex:pageBlockTable value="{!covid19}" var="c">
                 <apex:column value="{!c.country}"/>
                 <apex:column value="{!c.country_abbreviation}"/>
                 <apex:column value="{!c.total_cases}"/>
                 <apex:column value="{!c.new_cases}"/>
                 <apex:column value="{!c.total_deaths}"/>
                 <apex:column value="{!c.new_deaths}"/>
                 <apex:column value="{!c.total_recovered}"/>
                 <apex:column value="{!c.active_cases}"/>
                 <apex:column value="{!c.serious_critical}"/>
                 <apex:column value="{!c.cases_per_mill_pop}"/>
                 <apex:column value="{!c.flag}"/>   
         </apex:pageBlockTable>
     </apex:pageBlock>
</apex:form>
</apex:page>

Any suggestions on how to resolve this error... Thanks

  • I think your doFetchCOVIDInfo needs to return a PageReference type and you can return null – Scott Morrison May 6 at 12:00
  • @ScottMorrison i think page reference is only required when we want to jump to other VF page when something is clicked, and method takes you to new VF page. In this case, i am trying to display a List object on click of a button. – KKumar May 6 at 12:09
  • Which line in the method is throwing the error? – caleb.breckon May 6 at 12:46
  • Last line of the loop, covid19.add(covidCase)/ – KKumar May 6 at 13:09
1
public static list<WrapperCOVID19Info> covid19{get;set;}

This does not create an instance of a collection of WrapperCOVID19Info. You must either create the instance in the get; or simply check the null value in doFetchCOVIDInfo.

public List<WrapperCOVID19Info> covid19 {
    get {
        if (covid19 == null) {
            //covid19 = new List<WrapperCOVID19Info>();
        }
        return covid19;
    }
}

or

...
if(string.isNotBlank(covidCase.Country)){
    if (covid19 == null) covid19 = new List<WrapperCOVID19Info>();
    covid19.add(covidCase);
}
...
| improve this answer | |
  • 2
    covidWrapper = null should be covidWrapper == null – sfdcfox May 6 at 14:12
  • @sfdcfox thanks. I do like to rename the variables while analysing the code blocks to make it easier for my own reference!! – TSmith May 6 at 14:14
  • That worked, thanks @TSmith and sfdcfox – KKumar May 7 at 3:22

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.