<apex:page standardController="Account" extensions="myaccountcontroller">
<apex:form >
<apex:pagemessages ></apex:pagemessages>
<apex:commandButton value="Save" action="{!Save}"/>
</apex:form>
</apex:page>
public class myaccountcontroller
{
public string accId {get;set;}
public String finalamount {get;set;}
public myaccountcontroller(ApexPages.StandardController sc)
{
accId = ApexPages.CurrentPage().getParameters().get('Id');
doCalculation();
}
public void doCalculation()
{
Account AccObj = [SELECT Id,Name,Amount__c,Total_Amount__c FROM Account WHERE Id=:accId ];
system.debug('TotalAmount ###:'+AccObj.Total_Amount__c );
try{
finalamount = String.ValueOf(AccObj.Amount__c/0);
system.debug('finalamount###:'+finalamount);
}catch(Exception e)
{
if(e.getMessage() == 'System.NullPointerException: Attempt to de-reference a null object')
{
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Unite Price should not be empty'));
}
}
}
}
You can do it by using the rendered attribute on the button.
Make a class member on your controller called "hasError" which defaults to false. When you save, if you have an error, set it to true, otherwise set it to false. Make sure hasError has a getter.
In your VF page, add the following to the button:
rendered="{!not hasError}"