0

I have an apex class Class1 that searches for a specific account. On search, some account details with its related contacts will be displayed, as well as empty fields to create new opportunity for each selected contacts. This function works well as expected, and I don't have any problem with this.

At this point, I also created another apex class, Class2. This class is supposed to iterate through the list of selected contacts, so I called the GetSelectedContacts() method from the Class1 controller in it.

The problem is that every time I assert the list, is shows null value, and when I comment out that assert, displays this error: System.NullPointerException: Attempt to de-reference a null object Class.Class2.method1.

Class1

public class Class1 
{

    private ApexPages.StandardController controller;   
    public Object1__c obj1 {get;set;}
    public Account accountqry {get;set;}
    public List<contactwrapper> contactList {get;set;}
    public List<Contact> selectedContacts = new List<Contact>();
    public List<Opportunity> oppList {get;set;} 

    public Class1(ApexPages.StandardController con) {
        controller = con;
        obj1 = new Object1__c();
        accountqry = new Account(); 
        contactList = new List<contactwrapper>();
        oppList = new List<Opportunity>();
    }

    public void init() {

        if(obj1.Account__c != null) {
            accountqry = [SELECT Id, Name, Phone, Type FROM Account WHERE Id =: obj1.Account__c];
            contactList = getContacts();
        }

        else {
            obj1.clear();
            accountqry.clear();
            contactList.clear();
        }
    }


    public List<contactwrapper> getContacts()
    {

        for(Contact c : [select Id, FirstName, LastName from Contact WHERE AccountId =:  obj1.Account__c]) {
            contactList.add(new contactwrapper(c));
        }    
        return contactList;
    }

    public PageReference twoMethods() {
        getSelected();
        GetSelectedContacts();
        createOpp();
        return null;
    }

    public PageReference getSelected()
    {

        selectedContacts.clear();
        oppList.clear();
        for(contactwrapper conwrapper : contactList)
        if(conwrapper.selected == true)
        selectedContacts.add(conwrapper.con);
        return null;
    }

    public List<Contact> GetSelectedContacts()
    {

        if(selectedContacts.size()>0){
            return selectedContacts;   
        }    

        else {
            return null;

        } 


    } 

    public void createOpp() {

        if(selectedContacts.size() > 0) {

            oppList = new List<Opportunity>();

            for(Contact con : selectedContacts) {

                oppList.add(new Opportunity());

            }

        }    

    }


    public class contactwrapper
    {
        public Contact con{get; set;}
        public Boolean selected {get; set;}

        public contactwrapper(Contact c)
        {
            con = c;
            selected = false;
        }
    }
}

Class2

public class Class2 {

    Public List<Contact> selectedContactList = new List<Contact>(); 

    Public List<Contact> getSelectedContacts() {
            Object1__c ob1 = new Object1__c();
            ApexPages.StandardController sc = new ApexPages.StandardController(ob1);
            Class1 cl1 = new Class1(sc);  
            List<Contact> selectedContactList = ob1.GetSelectedContacts();   
            return selectedContactList;
    }

    public void method1() {
            List<Contact> selectedContactList2 = new List<Contact>();
            selectedContactList2 = getSelectedContacts();

            for(Contact c : selectedContactList2) {
             //some code
            }
    }

        //some methods...
}
0

Signature of your method is something like this :

public List<Contact> GetSelectedContacts()

The return type is List of Contact , where as the method is returning null in else statement which is probably why you are getting this error.Do let me know if this doesn't resolve your query.Please mark it as answer if it helped you so others can benefit

  • so should I comment out else in my statement? – eagerlearner Nov 3 '16 at 8:35
  • even if you comment out the else part and the if conditions doesnot qualify , the method will not return anything and you will get an error.For this particular situation ,u will need to get rid of if statement altogether else make sure that method return list of contact in every condition – Samir Nov 3 '16 at 8:37

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.