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
Jason McCarthy 2Jason McCarthy 2 

Apex Code Error: Adding Existing Contacts to Opportunities

I am trying to write an enhancement that adds existing contacts to opportunities (record type: Job Opportunities. ) 

I keep getting this error. 
Visualforce ErrorHelp for this Page
System.NullPointerException: Attempt to de-reference a null object
Error is in expression '{!addContact}' in component <apex:commandButton> in page addcontacttojobopportunity: Class.AddContacttoJobOpptController.addContact: line 30, column 1
Class.AddContacttoJobOpptController.addContact: line 30, column 1

VF Code: 
<apex:page controller="AddContacttoJobOpptController">
    <apex:form >
        <apex:pageBlock title="Search Existing Contact to Add">
            <apex:pageMessages />
            <apex:pageBlockButtons >
                <apex:commandButton value="Add Contact" action="{!addContact}"/>
                <apex:commandButton value="Go Back" action="{!goBack}"/>
            </apex:pageBlockButtons>
            <apex:pageBlockSection columns="1">             
                <apex:inputField label="Search Contact" value="{!Opp.JobOpportunity_Lookup__c}"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Controller Code: 
// Created by: Jason McCarthy
// Created for: Job Opportunities Project P#100007 
// Purpose: To add Current contacts to the Job Opportunity to Track candidates. 
// Created: September 29th, 2015


public with sharing class AddContacttoJobOpptController { 
    public Opportunity opp {get;set;}

    public Contact con {get;set;}
    public String selectedConId = '';

    public String selectedOppId = '';
  
    String selectedOpportunityID = null;

    public AddContacttoJobOpptController(){

   
   
        opp = new Opportunity();
    
 
        selectedOppId = Apexpages.currentPage().getParameters().get('aid');
            
        
    }
    public Pagereference addContact(){ 
        Set<String> conIdSet = new Set<String>();
        selectedConId = Con.Candidate__c ; .
        selectedOppId = Opp.JobOpportunity_Lookup__c ; 
       
        if(selectedConId != null && selectedConId != ''){
            for(Contact con : [select id from Contact where Candidate__c = :selectedOppId]){
                conIdSet.add(con.id);
            }
            if(conIdSet.contains(selectedConId)){
                apexpages.Message msg = new Apexpages.Message(ApexPages.Severity.Info,'The selected contact is already added to the Job Opportunity. Please select another contact to add.');
                apexpages.addmessage(msg);
                return null;
            }
            else{
                Contact con = new Contact(id=selectedConId);
                update con;
                apexpages.Message msg = new Apexpages.Message(ApexPages.Severity.Info,'The selected contact has been added to the Job Opportunity. Now you can select another contact to add.');
                apexpages.addmessage(msg);
                return null;
            }
            
        }
        else{
            apexpages.Message msg = new Apexpages.Message(ApexPages.Severity.Info,'Please select contact then click on Add Contact button.');
            apexpages.addmessage(msg);
            return null;
        }
        
    }
    public Pagereference goBack(){
        return new Pagereference('/'+selectedOppId);
    }

}

 
JeffreyStevensJeffreyStevens
On line#30 - you're using the Con variable.  It is null at line#30.  You don't instanceiate it until line #34
Jason McCarthy 2Jason McCarthy 2
Hi Jeffrey, 

Can you ellaborate on that?  Im not an expert in coding.
JeffreyStevensJeffreyStevens
Ya - At line#30 - you're doing selectedConId = Con.Candidate__c ;   - Well Con is an sObject - and it's only been defined, but instance of the variable has been created.  Therefor the value of Con is null.  Without digging into your code too much - I'd wrap line# with a if not null statement - like

if(Con.Candidate__c != null) {
selectedConId = Con.Candidate__c ; 
}

If that doesn't fix it - then do a try catch on it.

try {
selectedConId = Con.Candidate__c ; 
} catch (exception ex) {
selectedConId = '';
}