0

I am getting Attempt to de-reference a null object at below when del() method is called. Can anyone point out what has went wrong :

    public class DeletingCheckedRowsController {
        public List<WrapperClass> listWrapper {get;set;}
        public Boolean allBool {get;set;}
        public List<Project__c> searchedprojects{get;set;}
        public String projectname {get;set;}

    private ApexPages.StandardSetController setCon;
    private Set<Id> selectedContactIds;

    public DeletingCheckedRowsController() {
        this.selectedContactIds= new Set<Id>();
        this.setCon= new ApexPages.StandardSetController( [SELECT Id, Name, Project_Manager__c, AXA_BS_Project_Sponsor__r.Name, Start_Date__c, End_Date__c FROM Project__c] );
        this.setCon.setpageNumber(1);
        this.setCon.setPageSize(10);

     /*   listWrapper = new List<WrapperClass>();
        List<Project__c> listProj = [SELECT Id, Name, Project_Manager__c,AXA_BS_Project_Sponsor__r.Name, Start_Date__c, End_Date__c FROM Project__c];
        if(projectname !=null){
          listProj = [SELECT Id, Name, Project_Manager__c,AXA_BS_Project_Sponsor__r.Name, Start_Date__c, End_Date__c FROM Project__c where name like :'%'+projectname+'%'];  
        }
        if(listProj.size() > 0) {
            for(Project__c p : listProj) {
                listWrapper.add(new WrapperClass(p));
            }
        } */
    }

    public List<WrapperClass> getContacts(){

        List<WrapperClass> rows = new List<WrapperClass>();

        for(sObject r : this.setCon.getRecords()){
            Project__c c = (Project__c)r;

            WrapperClass row = new WrapperClass(c);
            if(this.selectedContactIds.contains(c.Id)){
                row.checked=true;
            }
            else{
                row.checked=false;
            }
            rows.add(row);
        }

        return rows;

    }

    public void doNext(){

        if(this.setCon.getHasNext())
            this.setCon.next();

    }

    /*
    *   advance to previous page
    */
    public void doPrevious(){

        if(this.setCon.getHasPrevious())
            this.setCon.previous();

    }

    public Boolean getHasPrevious(){

        return this.setCon.getHasPrevious();

    }

    /*
    *   return whether next page exists
    */
    public Boolean getHasNext(){

        return this.setCon.getHasNext();

    }

    public Integer getPageNumber(){

        return this.setCon.getPageNumber();

    }

    /*
    *    return total pages
    */
    Public Integer getTotalPages(){

        Decimal totalSize = this.setCon.getResultSize();
        Decimal pageSize = this.setCon.getPageSize();

        Decimal pages = totalSize/pageSize;

        return (Integer)pages.round(System.RoundingMode.CEILING);
    }

    public pagereference searchForProject(){
        listWrapper = new List<WrapperClass>();
        List<Project__c> listProj = [SELECT Id, Name, Project_Manager__c,AXA_BS_Project_Sponsor__r.Name, Start_Date__c, End_Date__c FROM Project__c];
        if(projectname !=null){
          listProj = [SELECT Id, Name, Project_Manager__c,AXA_BS_Project_Sponsor__r.Name, Start_Date__c, End_Date__c FROM Project__c where name like :'%'+projectname+'%'];  
        }
        if(listProj.size() > 0) {
            for(Project__c p : listProj) {
                listWrapper.add(new WrapperClass(p));

            }
        }
        return null;
    }

    public class WrapperClass {
        public Boolean checked {get;set;}
        public Project__c p {get;set;}
        public WrapperClass(Project__c p) {

            this.p = p;

        }
    }

    public void del() {
        List<Project__c> listProjForDel = new List<Project__c>();
        List<Project_Phase__c> listProjPhaseForDel = new List<Project_Phase__c>();
        List<Toll_Gate__c> listTollGateForDel = new List<Toll_Gate__c>();
        List<Task__c> listTaskForDel = new List<Task__c>();
        List<WrapperClass> listTempWrapper = new List<WrapperClass>();
        ***for(WrapperClass w : listWrapper) {
            if(w.checked) {
                listProjForDel.add(w.p);
            } else {
                listTempWrapper.add(w);
            }
        }***

        listProjPhaseForDel = [select Id from Project_Phase__c where Project__c IN :listProjForDel];
        listTollGateForDel = [select Id from Toll_Gate__c where Project_Phase__c IN :listProjPhaseForDel];
        listTaskForDel = [select Id from Task__c where Project__c IN :listProjForDel];

        if(listProjForDel.size() > 0) {
            delete listProjForDel;
            delete listProjPhaseForDel;
            delete listTollGateForDel;
            delete listTaskForDel;
            listWrapper = listTempWrapper;
        } else {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Select at least one Project to delete'));
        }
    }

    public void selectAll() {
        if(allBool) {
            for(WrapperClass w : listWrapper) {
                w.checked = true;
            }
        } else {
            for(WrapperClass w : listWrapper) {
                w.checked = false;
            }
        }
    }

   }

Regards,

2
  • What line of code does this error refer to?
    – Mark Pond
    Commented Mar 25, 2015 at 21:02
  • does searchForProjects() always get invoked before del() as del() requires an initialized listWrapper ?
    – cropredy
    Commented Mar 25, 2015 at 23:57

2 Answers 2

0

@CyberJus isEmpty() will not check for null. It will throw a null pointer exception if you do listProjForDel.isEmpty() when listProjForDel is null.

You can however use it in conjunction with checking for null


if (listProjForDel != null && !listProjForDel.isEmpty()) {
  delete listProjForDel;
}
1
  • 1
    Don't check if a list is empty before DML. It just wastes CPU time.
    – sfdcfox
    Commented Mar 25, 2015 at 21:28
0

Instead of

delete listProjForDel;

delete listProjPhaseForDel;

delete listTollGateForDel;

delete listTaskForDel;

You should try :

If(listProjForDel != null && listProjForDel.size() > 0)
delete listProjForDel;

If(listProjPhaseForDel!= null && listProjPhaseForDel.size() > 0)
delete listProjPhaseForDel;

If(listTollGateForDel!= null && listTollGateForDel.size() > 0)
delete listTollGateForDel;

If(listTaskForDel!= null && listTaskForDel.size() > 0)
delete listTaskForDel;
2
  • or just use !listProjForDel.isEmpty() - isEmpty() will check for null or 0 size.
    – CyberJus
    Commented Mar 25, 2015 at 20:51
  • Hi Jitendra. The error is throwing at wrapper class for loop for null reference.can u check if all the initialization done properly.i will be doing a mass delete from vf page with pagination.. Commented Mar 25, 2015 at 21:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .