+ Start a Discussion
Brian RomanowskiBrian Romanowski 

dereference a null object but object is not null

I'm trying to create a list of related ID's to a custom object, Base Elements. I check if the value is null in the SOQL and put in a conditioonal statement checking it again but when I go to add it to the list I get the System.NullPointerException: Attempt to de-reference a null object. I underlined the add statement that is causing the error. Any idea what is going on?

FOR (BMCServiceDesk__BMC_BaseElement__c b : [SELECT BMCServiceDesk__PrimaryClient__c FROM BMCServiceDesk__BMC_BaseElement__c WHERE BMCServiceDesk__PrimaryClient__c!=null ]) {
        system.debug('pc: '+b.BMCServiceDesk__PrimaryClient__c);
        if(b.BMCServiceDesk__PrimaryClient__c!=null){
            system.debug('this is not null!!');
            bepc.add(b.BMCServiceDesk__PrimaryClient__c);
        }
	}


Best Answer chosen by Brian Romanowski
Brian RomanowskiBrian Romanowski
Figured it out. I wasn't declaring the set of ID's correctly. I had
Set<ID> bepc;

instead of:
Set<ID> bepc = new set<ID>();


All Answers

Brian RomanowskiBrian Romanowski
Figured it out. I wasn't declaring the set of ID's correctly. I had
Set<ID> bepc;

instead of:
Set<ID> bepc = new set<ID>();


This was selected as the best answer
Apex - LearnerApex - Learner
Most likely you are not intializing the list (bepc) in which you are trying to add the element.

I do not see any decleration for this list (bepc), are you sure you are instantiating this list properly.

Please do let me know if this is not the case.