You need to sign in to do that
Don't have an account?

APEX / Visualforce Error - System.NullPointerExeption
Suspect this is a simple problem, but I can't see the wood for the trees.
I have two objects booking__c and traveller__c. When you create a booking record you have to enter a lead contact (lookup to contact) and then when you hit save you then add the travellers which appears as a related list on the booking record.
The first traveller is always the lead contact. The traveller record only has two fields the booking and the traveller (lookup to contact). So I was hoping to automatically create a traveller record as I have the booking record has the two ids needed.
I can successfully create a traveller record automatically linked to the booking without a traveller. It's only when I try to add the traveller (lookup to contact) I run into problems.
I have highlight in red the problem line. Have tried a few things like adding .id.
public class newBookingController { Booking__c booking; Traveller__c traveller; public Booking__c getBooking() { if(Booking == null) Booking = new Booking__c(); return booking; } public Traveller__c getTraveller() { if(Traveller == null) Traveller = new Traveller__c(); return Traveller; } public PageReference step1() { return Page.bookStep1; } public PageReference step2() { return Page.bookStep2; } public PageReference step3() { return Page.bookStep3; } public PageReference cancel() { PageReference bookingPage = new ApexPages.StandardController(booking).view(); bookingPage.setRedirect(true); return bookingPage; } public PageReference save() { insert booking; traveller.traveller__c = booking.lead_contact__c; traveller.booking__c = booking.id; insert traveller; PageReference bookingPage = new ApexPages.StandardController(booking).view(); bookingPage.setRedirect(true); return bookingPage; } }
Any suggestions welcome.
Thanks
Ross
I suspect what you want to do is this:
All Answers
I suspect what you want to do is this:
Thanks that worked, although I am unsure why I need to create the traveller record. I thought I did that in the code above.
Your code only creates a new travller object if getTraveller() is called.
During the execution of the code in the scenario you mentioned, getTraveller() won't get called, therefore you must create a new instance of a traveller object (otherwise it is null).
Thanks, still very new to this. That makes perfect sense.