You need to sign in to do that
Don't have an account?
Jason 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:
Controller Code:
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); } }
Can you ellaborate on that? Im not an expert in coding.
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 = '';
}