I'm having difficulty with a VF page I am building.
The objects involved are Opportunity, and Job_Bag_Contact__c (Junction object between Opp. (MD) and Contact(LKP)).
Here is my VF:
<apex:page standardController="Opportunity" extensions="JobBagContacts">
<apex:sectionHeader title="Job Bag Edit" subtitle="{!Opportunity.name}"/>
<apex:form >
<apex:pageBlock title="Job Bag Edit">
<apex:pageBlockButtons >
<apex:commandButton action="{!save}" value="Save"/>
<apex:commandButton action="{!delete}" value="Delete"/>
<apex:commandButton action="{!cancel}" value="Cancel"/>
</apex:pageBlockButtons>
<apex:pageBlockSection columns="2" title="Job Bag Information">
</apex:pageBlockSection>
<apex:pageBlockSection columns="2" title="Project or Customer Name & Registration">
</apex:pageBlockSection>
<apex:pageBlockSection columns="2" title="Accounting / Logistics Information">
</apex:pageBlockSection>
<apex:pageBlockSection columns="2" title="Shipping">
</apex:pageBlockSection>
<apex:pageBlockSection columns="2" title="Shipping Back Order">
</apex:pageBlockSection>
<apex:pageBlockSection columns="2" title="Custom Information">
</apex:pageBlockSection>
<apex:pageBlockSection columns="2" title="Additional Information">
</apex:pageBlockSection>
<apex:pageBlockSection columns="2" title="Description Information">
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
<apex:form >
<apex:pageBlock title="Job Bag Contacts">
<apex:variable value="{!0}" var="rowNumber" />
<apex:outputPanel id="panelWithVar">
<apex:variable value="{!0}" var="rowNumber" />
</apex:outputPanel>
<apex:pageBlockButtons >
<apex:commandButton value="Add" action="{!addNewObject}" reRender="JobBagContacts,NewJobBagContacts,panelWithVar" immediate="true"/>
<apex:commandButton value="Save" action="{!saveObjects}" reRender="JobBagContacts,NewJobBagContacts,panelWithVar" immediate="true"/>
<apex:commandButton value="Cancel" action="{!cancelChanges}" reRender="JobBagContacts,NewJobBagContacts,panelWithVar" immediate="true"/>
</apex:pageBlockButtons>
<apex:pageBlockTable value="{!contacts}" var="contact" id="NewJobBagContacts">
<apex:column headerValue="Name" title="Name" width="30%">
<apex:inputField value="{!contact.Contact__c}"/>
</apex:column>
<apex:column headerValue="Account" title="Account">
<apex:outputField value="{!contact.Contact__r.Account.Name}"/>
</apex:column>
<apex:column headerValue="Phone" title="Phone">
<apex:outputField value="{!contact.Contact__r.Phone}"/>
</apex:column>
<apex:column headerValue="Email" title="Email">
<apex:outputField value="{!contact.Contact__r.Email}"/>
</apex:column>
<apex:column headerValue="Title" title="Title">
<apex:outputField value="{!contact.Contact__r.Title}"/>
</apex:column>
<apex:column headerValue="Primary?" title="Primary?" width="10%">
<apex:inputField value="{!contact.Primary__c}"/>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
and here is the controller extension:
public with sharing class JobBagContacts {
public List<Job_Bag_Contact__c> objectsToInsert {get;set;}
public Integer numberOfRowsToRemove {get;set;}
Opportunity current {get;set;}
public JobBagContacts(ApexPages.StandardController controller) {
current = (Opportunity) controller.getRecord();
}
public List<Job_Bag_Contact__c> getContacts() {
return [
SELECT Contact__c, Contact__r.Account.Name, Contact__r.Phone, Contact__r.Email, Contact__r.Title, Primary__c
FROM Job_Bag_Contact__c
WHERE Opportunity__c = :current.Id
];
}
public PageReference addNewObject () {
system.debug('current: ' + current);
Job_Bag_Contact__c newObject = new Job_Bag_Contact__c (Opportunity__c = current.Id);
system.debug('newObject: ' + newObject);
objectsToInsert.add(newObject);
return null;
}
public PageReference removeNewObject () {
objectsToInsert.remove(numberOfRowsToRemove);
return null;
}
public PageReference saveObjects () {
if (objectsToInsert.size() > 0)
upsert objectsToInsert;
return null;
}
public PageReference cancelChanges () {
return null;
}
}
Basically it replicates the 'edit' page while allowing users to add, update or delete Job_Bag_Contact__c records on the same page.
Note, Opportunity has been re-labeled to Job Bag, it is not another custom object.
Thanks in advance for any help, I'm absolutely puzzled as to the cause of this error:
14:27:10.093 (93666000)|FATAL_ERROR|System.NullPointerException: Attempt to de-reference a null object
Class.JobBagContacts.addNewObject: line 23, column 1