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
Laytro80Laytro80 

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

Best Answer chosen by Admin (Salesforce Developers) 
Ryan-GuestRyan-Guest

I suspect what you want to do is this:

 

 

public PageReference save() {
    
      insert booking;
      
      traveller = new traveller__c();
      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; 
   }

All Answers

Ryan-GuestRyan-Guest

I suspect what you want to do is this:

 

 

public PageReference save() {
    
      insert booking;
      
      traveller = new traveller__c();
      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; 
   }
This was selected as the best answer
Laytro80Laytro80

Thanks that worked, although I am unsure why I need to create the traveller record.  I thought I did that in the code above.

Ryan-GuestRyan-Guest

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).

Laytro80Laytro80

Thanks, still very new to this. That makes perfect sense.