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
vijay kumar kvijay kumar k 

Attempt to de-reference a null object An unexpected error has occurred. Your development organization has been notified. In vf page

why error throws null object and plz help to solve this

vfpage:
<apex:page  controller="showallaccountsinvf">
    <apex:form>
        <apex:pageBlock >
            <apex:pageBlockSection title="accounts">
                <apex:pageBlockTable value="{!contactscount}" var="acon">
                    <apex:column headervalue="Name">
                        <apex:outputtext value="{!acon.acc.Name}"/>
                    </apex:column>
                    <apex:column headervalue="NUmber of Contacts">
                        <apex:outputtext value="{!acon.i}"/>
                    </apex:column>
                </apex:pageBlockTable>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>
controller:
public class showallaccountsinvf {
    public map<id,account> accMap{set;get;}
    public list<wrappercontact> contactscount {set;get;}
    public showallaccountsinvf(){
        List<account> AccountList=[select id,name,contactslist__c from account];
        contactscount=new list<wrappercontact>();
        for(Account a:AccountList){
            accMap.put(a.id,a);
        }
        list<aggregateresult> aggrResults=[select count(id) countids,AccountId from contact where accountid =:accMap.keyset() group by accountid];
        for(aggregateresult agg:aggrResults){
            String acId=String.valueOf(agg.get('AccountId'));
            integer conid=integer.valueof(agg.get('countids'));
            Account a1=accMap.get(acId);
            contactscount.add(new wrappercontact(a1,conid));
            
            system.debug('@@@@@@@@'+acId+conid);
        }
    }
    public class wrappercontact
    {
        public Account acc{get;set;}
        public integer i{get;set;}
        
        Public wrappercontact(Account aa,integer j)
        {
            acc = aa;
            i = j;
        }
    }    
}
Deepali KulshresthaDeepali Kulshrestha

Hi vijay,

The problem in your code is that you have not initialized the Map that's why this error occurs.

Please add this line in your constructor.
accMap=new Map<Id,account>();

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha