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:outputField value="{!Page_View__c.Count__c}" />

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');
        viewList = [select id, Count__c from    Page_View__c where id =: CurrentRecordID];
    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++;
        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

  • What's on line 20?
    – Nick Cook
    Sep 1 '16 at 12:38

It looks like the error is happening here:


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++;
  • 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 '16 at 12:42
  • Martin it is Working Thank You,But why you are giving like this ,Please Explain
    – SFDC
    Sep 1 '16 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 '16 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 '16 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 '16 at 12:51

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.