1

I have Created the Visualforce Page and apex class which Means Controller Class to count the Viewers of users using the Visualforce Page

<apex:page sidebar="false" showheader="false" StandardController="Page_View__c" extensions="DisplayPage" action="{!TotalRecordUpdate}">
    <apex:pageBlock >
        <apex:form />
        <apex:pageBlockSection >
            title="your record details of ID:{!CurrentRecordID}"
        </apex:pageBlockSection>
    </apex:pageBlock>

    <apex:outputField value="{!Page_View__c.Count__c}" />
</apex:page>

Apex Class;

public class DisplayPage {
    public string CurrentRecordID {get;set;}
    public  list<Page_View__c> viewList {get;set;}
    public static string CurrentRecordID1 {get;set;}
    public DisplayPage(ApexPages.StandardController Controller) {
        CurrentRecordID = ApexPages.currentPage().getparameters().get('id');
        system.debug(CurrentRecordID);
        viewList = [select id, Count__c from    Page_View__c where id =: CurrentRecordID];
        system.debug(viewList);
        DisplayPage.TotalRecordUpdate();
    }
    public static void TotalRecordUpdate() {
        CurrentRecordID1 = ApexPages.currentPage().getparameters().get('id');
        List <  Page_View__c > totalupdate = new List < Page_View__c > ();
        List <  Page_View__c > recDetails = [select id, Count__c from   Page_View__c where id =: CurrentRecordID1];
        for (   Page_View__c updateCount: recDetails)
        {
            updateCount.Count__c = updateCount.Count__c++;
            totalUpdate.add(updateCount);
        }
        update totalUpdate;
    }
}

**I am Getting the Error: Visualforce Error

Help for this Page
System.NullPointerException: Attempt to de-reference a null object Error is in expression '{!TotalRecordUpdate}' in component in page viewingvisualforce: Class.DisplayPage.TotalRecordUpdate: line 20, column 1 Class.DisplayPage.TotalRecordUpdate: line 20, column 1**

How to Rectify the Error anyone Guide Me

1
  • What's on line 20?
    – Nick C
    Sep 1, 2016 at 12:38

1 Answer 1

2

It looks like the error is happening here:

updateCount.Count__c++;

There are some Page_View__c records that don't have a value for Count__c, and when you try to increment the null value, the system throws an error.

You should check to see if the value is null before performing the increment:

updateCount.Count__c = updateCount.Count__c == NULL ? 1 : updateCount.Count__c++;
5
  • 1
    Wouldn't another option be just to set the default value for the count__c field to 0? Then it wouldn't be null and also wouldn't affect the accuracy of the count.
    – cmmoutes13
    Sep 1, 2016 at 12:42
  • Martin it is Working Thank You,But why you are giving like this ,Please Explain
    – SFDC
    Sep 1, 2016 at 12:42
  • @cmmoutes13 It should work either way. If the code relies on a value being present, just make sure the data is there. Set default values, update historic records, etc. and you should be fine.
    – martin
    Sep 1, 2016 at 12:47
  • Okay that is what I was thinking, just wanted to make sure that that would be an option for the original poster. Thanks!
    – cmmoutes13
    Sep 1, 2016 at 12:50
  • @user36188 The terniary operator that I used in my answer provides if-then conditional functionality. The part before the ? takes a boolean expression, which in this case is the check for the Count__c field being NULL. The part immediately after the ? is the action when the test condition is true. In this case it gives a value of 1. The part after the : is the action if the test condition is false, which in this case is executed when the field Count__c is not NULL, and it performs the increment on the already existing value there.
    – martin
    Sep 1, 2016 at 12:51

You must log in to answer this question.

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