-1

When I'm trying to open the VF I'm getting the error :

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

The following is my VF Page :

<apex:page controller="AccountTable" sidebar="false" showHeader="false" >
<apex:form id="myform">
    <apex:pageBlock >
       Please Select A Dealer To Add As Participating Dealer : <apex:selectList value="{!regionValue}" size="1" >
       <apex:actionSupport event="onchange" action="{!dropDown}" rerender="myform"/>
       <apex:selectOptions value="{!regionOptions}"/> 
       </apex:selectList>
    </apex:pageBlock>
    <apex:pageBlock id="table" rendered="{!if(panelStopper = true,true,false)}">
        Filter 
       <p> Dealer Type     <apex:inputText value="{!dealerType}"/> </p>
       <p>  Dealer Code    <apex:inputText value="{!dealerCode}"/> </p>
        <apex:pageBlockButtons location="Bottom">
            <apex:commandButton value="Search" action="{!SearchLogic}" reRender="myform" />
        </apex:pageBlockButtons>
    </apex:pageBlock>

    <apex:outputPanel id="accounts">
        <apex:pageblock >
            <apex:pageblocktable value="{!Account}" var="cc" id="page">
                <apex:column headerValue="Choose Account">
                    <apex:inputCheckbox value="{!cc.bool}"/>
                </apex:column><apex:column value="{!cc.con.Name}"/>
                <apex:column value="{!cc.con.City__c}"/>
                <apex:column value="{!cc.con.Dealer_GC_Code__c }"/>
                <apex:column value="{!cc.con.Phone  }"/>
            </apex:pageblocktable>
            <apex:pageBlockButtons location="Bottom">
                <apex:commandButton value="<<" action="{!first}" reRender="accounts" />
                <apex:commandButton value="<" action="{!previous}" reRender="accounts" />
                <apex:commandButton value=">" action="{!next}" reRender="accounts" />
                <apex:commandButton value=">>" action="{!last}" reRender="accounts" />
            </apex:pageBlockButtons>
            <apex:outputText >Page Number {!pageNumber} </apex:outputText>
            <apex:pageBlockButtons location="bottom">
                <apex:commandButton value="Add" action="{!clickMe}" />
                <apex:commandButton value="Cancel"  onclick="window.parent.box.hide()" /> 
            </apex:pageBlockButtons>
        </apex:pageblock>
    </apex:outputPanel>
    <apex:pageblock >
        <apex:outputPanel rendered="{!display}">
            <apex:pageblocktable value="{!selectedList}" var="w" >
                <apex:column value="{!w.Name}"/>
            </apex:pageblocktable>
        </apex:outputPanel>
    </apex:pageblock>
</apex:form>

The following is my controller page :

Public class AccountTable{
public String size { get; set; }

//This is Our collection of the class/wrapper objects AddingParticipatingDealer 
Public List<AddingParticipatingDealer> wrapperlist{get;set;}
Public Integer noOfRecords{get; set;}

// Create a new Map to verify whether the Account is already added in the Map
Map <id,Account> SelectedAccountMap = new Map <id,Account>();

public boolean display{get;set;}
public list<Account> selectedList {get;set;}
public String regionValue {get;set;}    
public List<SelectOption> regionOptions {get;set;}
public String dealerType {get;set;}
public String dealerCode {get;set;} 
public Boolean panelStopper {get;set;}

public AccountTable(){ 
    regionOptions = new List<SelectOption>();
    regionOptions.add(new SelectOption('aid','All Dealer'));
    regionOptions.add(new SelectOption('sid','Select A Dealer'));
    panelStopper = false;
}

public ApexPages.StandardSetController setCon {get;set;}

//Returns a list of wrapper objects for the sObjects in the current page set
Public Void dropDown(){
    if(regionValue == 'sid' ){
        panelStopper = true;
    } 
    else{
        panelStopper = false;
    }
}

Public void SearchLogic(){
    Setcon = new ApexPages.StandardSetController(Database.getQueryLocator([Select Name,Dealer_Type__c,Dealer_GC_Code__c from Account Where Dealer_Type__c = :dealerType AND Dealer_GC_Code__c=:dealerCode]));
    setCon.setpagesize(10);
    noOfRecords = setCon.getResultSize();
    getAccount();
}

Public List<AddingParticipatingDealer> getAccount(){
    getSelectedAccount();
    wrapperlist = new List <AddingParticipatingDealer >();
    //The below line number is 48
    for(Account cc : (List<Account>)Setcon.getRecords()){ //In debug log it was showing error at this line
        AddingParticipatingDealer wrapAcc = new AddingParticipatingDealer();
        wrapAcc.con = cc;               // Holding Account
        if(SelectedAccountMap.containsKey(cc.Id)){
            wrapAcc.bool = true;    // check if Checkbox is checked in vf page    
        }
        else{
            wrapAcc.bool = false;    // Used as Checkbox in vf page, default is not selected
        }
        wrapperlist.add(wrapAcc);
    }
    return wrapperlist;
}

public void getSelectedAccount(){
    if(wrapperlist!=null){
        for(AddingParticipatingDealer  wr:wrapperlist){
            if(wr.bool == true){
                SelectedAccountMap.put(wr.con.id,wr.con); // Add the selected Account id in to 
            }
            else{
                SelectedAccountMap.remove(wr.con.id); // If you uncheck the Account, remove it from the selectedAccountMap
            }
        }

    }

}

public void first(){
    Setcon.first();
    system.debug('++++++++++++++First'+Setcon);
}

 // returns the last page of records
 public void last() {
     Setcon.last();
 }

// returns the previous page of records
 public void previous() {
     Setcon.previous();
 }

//disable first and prev button
public Boolean hasPrevious {
    get {
        return Setcon.getHasPrevious();
    }
    set;
}

//display the next page of records
public void next() {
    Setcon.next();
    system.debug('++++++++++++++Next'+Setcon);
}

//disable the next and last buttons
public Boolean hasnext {
    get {
        return Setcon.getHasNext();
    }
    set;
}

public void clickMe(){
    display = true;
    getSelectedAccount();
    selectedList = SelectedAccountMap.values();
}

public integer pageNumber{
    get{
        return Setcon.getPageNumber();
    }
    set;
}

Public class AddingParticipatingDealer{
    Public Account con{get;set;}
    Public boolean bool{get;set;}
}

}

I'm getting the error at line 48 column 1

2

It's because setCon is still null at the point where you're trying to access it. You can fix this by updating your constructor:

public AccountTable(){ 
    regionOptions = new List<SelectOption>();
    regionOptions.add(new SelectOption('aid','All Dealer'));
    regionOptions.add(new SelectOption('sid','Select A Dealer'));
    panelStopper = false;
    SearchLogic();
}
  • That's working.. and can you please explain me in detail why this happen. – Lokesh Mar 23 '17 at 16:54
  • @Lokesh You have to initialize a variable before you can use it. The constructor is the typical place where one would initialize their variables. – sfdcfox Mar 23 '17 at 17:03

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.