You need to sign in to do that
Don't have an account?
sml9099
Display error message on VisualForce page
Hey Guys,
I have created a visualforce page which displays the list of records which meet the critiriea on the page. Along with this , there are some fields which are performaing some calculations. I am trying to display error message when it doesn't mee the critirea. It is working fine when there is no calculation field but when I include the calculation field , it gives me this error. In short, If I remove the Highlighted line in apex code it works fine but I do want to include that line also. Please help.
Attempt to de-reference a null object
Error is in expression '{!FetchSPRec}' in component <apex:commandButton> in page trial_rfp_page
Calculation field :- MaxImpression
Apex code:
public class Fetchsiteplacement12{
public Site_Placements__c sp{get;set;}
public List<Site_Placements__c > spRec{get;set;}
String matchString;
String matchString1;
public integer NumberOfSitePlacements {get;set;}
public decimal SumOfImpressions{get;set;}
public decimal MaxImpressions{get;set;}
public Fetchsiteplacement12(){
sp=new Site_Placements__c ();
spRec = new List<Site_Placements__c>();
matchString = '';
matchString1 ='';
}
public void FillAggregates()
{
NumberOfSitePlacements = [ Select count()
FROM Site_Placements__c where ((Device_Type__c like :matchString) AND (Auto_Play__c like :matchString1))];
SumOfImpressions = (decimal)([ SELECT sum(Total_Impresions__c)
FROM Site_Placements__c Where ((Device_Type__c like :matchString) AND (Auto_Play__c like :matchString1))][0].get('expr0'));
MaxImpressions = SumOfImpressions/NumberOfSitePlacements ;
}
public void FetchSPRec(){
matchString = '%'+sp.Device_Type__c+'%';
matchString1 = '%'+sp.Auto_Play__c+ '%';
spRec=[select Name, Site_Placement_ID__c, Site_Level_Max_Impressions__c from Site_Placements__c where
((Device_Type__c like :matchString) AND (Auto_Play__c like :matchString1) ) ORDER BY Site_Level_Max_Impressions__c DESC limit 10];
if(spRec.size() == 0)
{
Apexpages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO,''+'No records to Display"'));
}
FillAggregates();
}
public pagereference CancelSPRec(){
PageReference page = new PageReference('https://cs1.salesforce.com/a36/o');
page.SetRedirect(true);
return page;
}}
VisualForce page:-
<apex:page controller="Fetchsiteplacement12" tabStyle="Site_Placements__c" sidebar="True">
<apex:form >
<apex:pageBlock >
<apex:messages />
<apex:pageBlockButtons location="Both">
<apex:commandButton value="Fetch" action="{!FetchSPRec}"/>
<apex:commandButton value="Cancel" action="{!CancelSPRec}"/>
</apex:pageBlockButtons>
<apex:pageBlockSection title="Please select the Critirea">
<apex:pageBlockSectionItem >
<apex:outputLabel value="Auto Play" for="autoplay"/>
<apex:inputField value="{!sp.Auto_Play__c}" id="autoplay"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputLabel value="Device Type" for="devicetype"/>
<apex:inputField value="{!sp.Device_Type__c}" id="devicetype"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSection Title="Calculations">
<apex:pageBlockSectionItem > Number of site placements : {!NumberOfSitePlacements}<br/></apex:pageBlockSectionItem>
<apex:pageBlockSectionItem > Max Impressions : {!Maximpressions}<br/></apex:pageBlockSectionItem>
</apex:pageBlockSection>
<apex:pageBlockSection title="Results">
<apex:pageBlockSectionItem >
<apex:pageBlockTable value="{!spRec}" var="site" >
<apex:column value="{!site.Name}"/>
<apex:column value="{!site.Site_Placement_ID__c}"/>
<apex:column value="{!site.Site_Level_Max_Impressions__c}"/>
</apex:pageBlockTable>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
</apex:pageblock>
</apex:form>
</apex:page>
I have created a visualforce page which displays the list of records which meet the critiriea on the page. Along with this , there are some fields which are performaing some calculations. I am trying to display error message when it doesn't mee the critirea. It is working fine when there is no calculation field but when I include the calculation field , it gives me this error. In short, If I remove the Highlighted line in apex code it works fine but I do want to include that line also. Please help.
Attempt to de-reference a null object
Error is in expression '{!FetchSPRec}' in component <apex:commandButton> in page trial_rfp_page
Calculation field :- MaxImpression
Apex code:
public class Fetchsiteplacement12{
public Site_Placements__c sp{get;set;}
public List<Site_Placements__c > spRec{get;set;}
String matchString;
String matchString1;
public integer NumberOfSitePlacements {get;set;}
public decimal SumOfImpressions{get;set;}
public decimal MaxImpressions{get;set;}
public Fetchsiteplacement12(){
sp=new Site_Placements__c ();
spRec = new List<Site_Placements__c>();
matchString = '';
matchString1 ='';
}
public void FillAggregates()
{
NumberOfSitePlacements = [ Select count()
FROM Site_Placements__c where ((Device_Type__c like :matchString) AND (Auto_Play__c like :matchString1))];
SumOfImpressions = (decimal)([ SELECT sum(Total_Impresions__c)
FROM Site_Placements__c Where ((Device_Type__c like :matchString) AND (Auto_Play__c like :matchString1))][0].get('expr0'));
MaxImpressions = SumOfImpressions/NumberOfSitePlacements ;
}
public void FetchSPRec(){
matchString = '%'+sp.Device_Type__c+'%';
matchString1 = '%'+sp.Auto_Play__c+ '%';
spRec=[select Name, Site_Placement_ID__c, Site_Level_Max_Impressions__c from Site_Placements__c where
((Device_Type__c like :matchString) AND (Auto_Play__c like :matchString1) ) ORDER BY Site_Level_Max_Impressions__c DESC limit 10];
if(spRec.size() == 0)
{
Apexpages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO,''+'No records to Display"'));
}
FillAggregates();
}
public pagereference CancelSPRec(){
PageReference page = new PageReference('https://cs1.salesforce.com/a36/o');
page.SetRedirect(true);
return page;
}}
VisualForce page:-
<apex:page controller="Fetchsiteplacement12" tabStyle="Site_Placements__c" sidebar="True">
<apex:form >
<apex:pageBlock >
<apex:messages />
<apex:pageBlockButtons location="Both">
<apex:commandButton value="Fetch" action="{!FetchSPRec}"/>
<apex:commandButton value="Cancel" action="{!CancelSPRec}"/>
</apex:pageBlockButtons>
<apex:pageBlockSection title="Please select the Critirea">
<apex:pageBlockSectionItem >
<apex:outputLabel value="Auto Play" for="autoplay"/>
<apex:inputField value="{!sp.Auto_Play__c}" id="autoplay"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputLabel value="Device Type" for="devicetype"/>
<apex:inputField value="{!sp.Device_Type__c}" id="devicetype"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSection Title="Calculations">
<apex:pageBlockSectionItem > Number of site placements : {!NumberOfSitePlacements}<br/></apex:pageBlockSectionItem>
<apex:pageBlockSectionItem > Max Impressions : {!Maximpressions}<br/></apex:pageBlockSectionItem>
</apex:pageBlockSection>
<apex:pageBlockSection title="Results">
<apex:pageBlockSectionItem >
<apex:pageBlockTable value="{!spRec}" var="site" >
<apex:column value="{!site.Name}"/>
<apex:column value="{!site.Site_Placement_ID__c}"/>
<apex:column value="{!site.Site_Level_Max_Impressions__c}"/>
</apex:pageBlockTable>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
</apex:pageblock>
</apex:form>
</apex:page>
if(spRec.size() == 0)
{
Apexpages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO,''+'No records to Display"'));
}
FillAggregates();
So FillAggregates gets called even if there are no records. You should change it to:
if(spRec.size() == 0)
{
Apexpages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO,''+'No records to Display"'));
} else {
FillAggregates();
}
Now FillAggregates will only be called if there are records in spRec.
All Answers
I checked by using debug statements. They have a valid value. Do you have any idea whats going on ?
Thanks
in method or in constructor
public Fetchsiteplacement12(){
sp=new Site_Placements__c ();
spRec = new List<Site_Placements__c>();
matchString = '';
matchString1 ='';
MaxImpressions = null;
or
MaxImpressions = 0.0;
}
public void FillAggregates()
{
MaxImpressions = null;
or
MaxImpressions = 0.0;
NumberOfSitePlacements = [ Select count()
FROM Site_Placements__c where ((Device_Type__c like :matchString) AND (Auto_Play__c like :matchString1))];
SumOfImpressions = (decimal)([ SELECT sum(Total_Impresions__c)
FROM Site_Placements__c Where ((Device_Type__c like :matchString) AND (Auto_Play__c like :matchString1))][0].get('expr0'));
MaxImpressions = SumOfImpressions/NumberOfSitePlacements ;
}
Thanks for your reply. But I am still getting the same error.
Attempt to de-reference a null object
Error is in expression '{!FetchSPRec}' in component <apex:commandButton> in page trial_rfp_page
MaxImpressions = SumOfImpressions/NumberOfSitePlacements ;
What happens if you try
System.debug("SumOfImpressions/NumberOfSitePlacements: "+SumOfImpressions/NumberOfSitePlacements):
When i use the above debug statement ,and I check the debug logs . It gives me value.
12:33:05.191 (191418000)|SYSTEM_METHOD_ENTRY|[74]|System.debug(ANY)
12:33:05.191 (191423000)|USER_DEBUG|[74]|DEBUG|SumOfImpressions/NumberOfSitePlacements: 94985.6666666666666666666666666667
12:33:05.191 (191428000)|SYSTEM_METHOD_EXIT|[74]|System.debug(ANY)
But I want that if code doesnt meet the critirea , it should not display the list of records and the error message " No records found". It displays the error message when I dont include this MaxImpressions = SumOfImpressions/NumberOfSitePlacements. But when I include this calculation field it doesn't show the error message , instaed it displays
Attempt to de-reference a null object
Error is in expression '{!FetchSPRec}' in component <apex:commandButton> in page trial_rfp_page
if(spRec.size() == 0)
{
Apexpages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO,''+'No records to Display"'));
}
FillAggregates();
So FillAggregates gets called even if there are no records. You should change it to:
if(spRec.size() == 0)
{
Apexpages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO,''+'No records to Display"'));
} else {
FillAggregates();
}
Now FillAggregates will only be called if there are records in spRec.
You are genius. It worked like a charm. Thanks again for your help.
Thanks