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
Walter@AdicioWalter@Adicio 

where is this causing "attempt to de-reference null object" ?

 

i need helping finding why i am getting that error. and also is there a way i can identify the actual record causing it instead of the output just saying that error ?

 

 

public void doPhoneComparison(){
		
		try{
		
		// sf contact phone numbers
		map<string,string> map1 = new map<string,string>();
		
		// phone numbers obj phone numbers
		map<string,string> map2 = new map<string,string>();
		
		// get all contact phone numbers, there is going to be over 7000
		integer i=0;
		
			ApexPages.addMessage( new ApexPages.Message(
    		ApexPages.Severity.INFO  , datetime.now() +' checking phone number field...'));

        	ApexPages.addMessage( new ApexPages.Message(
    		ApexPages.Severity.INFO  , datetime.now() +' checking external phone numbers...'));

  		// get all 'phone number' obj phone numbers
  		for(Phone_Numbers__c p : [select phone_number__c from Phone_Numbers__c]){
  			
  			p.phone_number__c = p.phone_number__c.replaceAll('-','');
			p.phone_number__c = p.phone_number__c.replaceAll('\\(','');
			p.phone_number__c = p.phone_number__c.replaceAll('\\)','');
			p.phone_number__c = p.phone_number__c.replaceAll(' ','');

              map2.put(p.phone_number__c,p.phone_number__c);
        }
        
        	ApexPages.addMessage( new ApexPages.Message(
    		ApexPages.Severity.INFO  , datetime.now() +' comparing phone numbers...'));
  
       // compare the two numbers
         for(Contact c : [select id,phone from Contact where id != null AND phone != null]){
 
         	if(c!=null && c.phone!=null && c.phone.length()>0){
	         	if(c.phone.contains('-')==true){
	         		c.phone = c.phone.replaceAll('-','');
	         	}
	         	
	         	if(c.phone.contains('(')==true){
					c.phone = c.phone.replaceAll('\\(','');
	         	}
				
				if(c.phone.contains(')')==true){
					c.phone = c.phone.replaceAll('\\)','');
				}
				
				if(c.phone.contains(' ')==true){
					c.phone = c.phone.replaceAll(' ','');
				}
         	}

		 //   if (map1.get(map2.get(c.phone)) == c.phone ) 
		  if (map2!=null && map2.get(c.phone) == c.phone ) 
		    { 
		    	// output matches
		        ApexPages.addMessage( new ApexPages.Message(
    			ApexPages.Severity.INFO  ,'found match:  contact phone ' +c.phone +' , external phone ' +map2.get(c.phone) +' '));
		   		i++;
		    } 
	
		} 
       
		// output count of records processed
		ApexPages.addMessage( new ApexPages.Message(
    	ApexPages.Severity.INFO  ,'matches ' +i ));
    	
		} catch(NullPointerException e){
			ApexPages.addMessages(e);
		}
	}

 

Best Answer chosen by Admin (Salesforce Developers) 
Walter@AdicioWalter@Adicio

thank you JA-Dev i will try your suggestion about looking at the debug log.

 

i got rid of the error by making a string equal the phone number and then if the string is null i add '000000000' before working with the string. i.e.

 

string s = c.phone;

 

if (s==null){

  s = '0000000';

}

 

then i work on  's'

All Answers

JA-DevJA-Dev

Doesn't it give you a line number? Anyway, try to output data by using System.debug('data you want to see goes here').

 

If I were to guess, I would say your first query is returning null values : 

 

for(Phone_Numbers__c p : [select phone_number__c from Phone_Numbers__c]){

 But again, since I can't see your data, I have no idea. Try to exclude null values in that query or see if there are records with null phone_number__c values.

 

Walter@AdicioWalter@Adicio

thank you JA-Dev i will try your suggestion about looking at the debug log.

 

i got rid of the error by making a string equal the phone number and then if the string is null i add '000000000' before working with the string. i.e.

 

string s = c.phone;

 

if (s==null){

  s = '0000000';

}

 

then i work on  's'

This was selected as the best answer