0

enter image description hereLooking to track the thumbsup button clicks by increment in the object but stuck up with the null point exception. need help

Code :

global with sharing class CountThumbsUpDownComments {
    public String language                              {get;set;}
    public string country                               {get;set;}
    public string faqid                                 {get;set;}
    public List<string> thumpsUp                        {get;set;}
    public List<string> thumpsdown                      {get;set;}
    public string comment                               {get;set;}
    public static List<FAQ_User_Rating_Summary__c> faqSummary;
    public static integer count = 0;

    public CountThumbsUpDownComments(){
         language = apexpages.currentpage().getparameters().get('lang');
         country = apexpages.currentpage().getparameters().get('country');
         faqid = apexpages.currentpage().getparameters().get('id');
         //faqSummary = new List<FAQ_User_Rating_Summary__c>();
    }
    @RemoteAction
    global static Integer ThumbsUpCount(String language, String country, String faqid) {
        FAQ_User_Rating_Summary__c fq = new FAQ_User_Rating_Summary__c();
        fq.Country__c = 'country';
        fq.FAQ_Id__c= 'faqid';
        fq.Language__c= 'language';
        fq.Thumps_Down_Count__c = count + 1;
        Upsert fq;
        return null;
    }
7
  • 1
    Are you able to call the remoteAction Function? Which line your getting null pointer Exception.
    – NITHESH K
    Aug 16, 2017 at 7:43
  • You are getting NPE as you are passing null value to Integer.valueOf. You need to either retrieve the existing record with that field or start with fq.Thumps_Up_Count__c = 0
    – jagmohan
    Aug 16, 2017 at 7:50
  • I have faqid as external id, am upserting the thumps up, based on the faqid it should update the count. PFA the screenshot where the values are getting populated when am inserting the data through anonymous window.
    – B1 sfdc
    Aug 16, 2017 at 8:34
  • Look like there is no problem in controller, Add more code of visualforce.
    – NITHESH K
    Aug 16, 2017 at 9:12
  • Also put system.debug and check whether remote action function is get called properly.
    – NITHESH K
    Aug 16, 2017 at 9:12

2 Answers 2

0

In your @RemoteAction method below line will always return Null pointer exception as fq.Thumps_Up_Count__c is never initialized with a value.

fq.Thumps_Down_Count__c = Integer.valueOf(fq.Thumps_Up_Count__c) + 1;

To avoid this, initialize fq.Thumps_Up_Count__c with some value first as or if you want to update count on an already created FAQ_User_Rating_Summary__c record, fetch it with an SOQL query.

Also as I see in your @RemoteMethod you are passing some parameters from VF page but not binding values of those parameters to new record. shouldn't it be like this?

@RemoteAction
global static Integer ThumbsUpCount(String language, String country, String faqid) {
        FAQ_User_Rating_Summary__c fq = new FAQ_User_Rating_Summary__c();
        // Look for Existing FAQ Rating Summary
        list<FAQ_User_Rating_Summary__c> existingFAQ = new list<FAQ_User_Rating_Summary__c>([SELECT Id, Thumps_Down_Count__c FROM FAQ_User_Rating_Summary__c WHERE FAQ_Id__c=:faqid limit 1]); // Assuming there will be one Rating summary per FAQ.
        if( !existingFAQ.isEmpty() ){
            fq = existingFAQ.get(0);
            fq.Thumps_Down_Count__c = fq.Thumps_Down_Count__c != null ? fq.Thumps_Down_Count__c + 1 : 1; 
        }else{
            fq.Country__c = country;
            fq.FAQ_Id__c= faqid;
            fq.Language__c= language;
            fq.Thumps_Down_Count__c = 1;
        }
        upsert fq;
        return fq.Thumps_Down_Count__c ;
}

I hope this helps :-)

0

I think this line is resulting into the error! As fq.Thumbs_Up_Count do not have any value. Either set its default value to 0 by editing field.

    fq.Thumps_Down_Count__c = Integer.valueOf(fq.Thumps_Up_Count__c) + 1;

You must log in to answer this question.

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