function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Developer IronSourceDeveloper IronSource 

getter method in visualforce throwing an error

Hi,
I have a visualforce which has some buttons.
The buttons are rendered according to the getter methods in the controller.

For Example:
The VF:
<apex:commandbutton rendered="{!EditButtonVisibility}" styleclass="slds-button slds-button--neutral" action="{!changeEditMode}" status="processStatus" value="Edit" />
The controller:
public Boolean getEditButtonVisibility() {
   if (obj.Approval_Status__c != null && 
         obj.Approval_Status__c.equalsIgnoreCase('Pending')){
              return true;
     }
     return false;
}

The problem is that sometimes I get a null pointer exception inside the method. Probably the obj variable is null but it's not null for certain because the whole page is surrounded by it.
I tried putting a try and catch but I can't do nothing inside the catch .. I can't do dml functions inside a getter as well as calling a future method, So I don't know how to monitor it.

It should be noted that the null pointer exception is not happaning all the time.
The only thing I can think about is that it's happaing due to poor network connection which causing the obj to be null.
 
Any idea how to monitor or solving that problem ?
Thanks.
pconpcon
Without seeing the whole controller and VF I can say for sure.  If the the obj is a class level variable that is set in the constructor, then network connectivity is not going to be a problem since it would be set prior to the page being loaded.  If you want to put the whole page and controller here I can take a look and see if I come up with any other ideas.

As for monitoring, we've done some stuff with google analytics and / or loggly to dump data on actions to them.  You could wirte a javascript method that fires on page load and dumps the contents of your obj object as well as other data if the obj object is null.  Then just update your method to have a null check for obj so that your users don't get the error message. 
Developer IronSourceDeveloper IronSource
Thanks pcon,
The vf+controller are very long so it's impossible to put it here.
 
Notice that it's a getter method which happans on page load (it's responsible for rendering the button), that's why I think it might be connectivity issue.

About Google Analytics / Loggle - do you have an example of how to implement it ?
pconpcon
I don't have a good example for how you could do it, but you could create a method in your controller that checks to see if the obj is null and returns true.  If it is true then render to the page a javascript chunk that uses the Exception Tracking [1] in google anayltics to send whatever data you think might be interesting.  I'd probably start with user id and then add on any driving data behind the obj.

[1] https://developers.google.com/analytics/devguides/collection/analyticsjs/exceptions