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
Katie KourtakisKatie Kourtakis 

visualforce form for custom object

Hello, I am fairly new to developing in Salesforce and am trying to create a form for a custom object and insert the information into the system. I have the form and custom controller created but it will not insert the newly created record into the database. I get the error "attempt to de-reference a null object". Any help would be appreciated.

Visualforce Page

Apex Class

Actual Page
Best Answer chosen by Katie Kourtakis
Rahul.MishraRahul.Mishra
Hi Katie,

Update your controller with following code:
 
public with sharing class ReservationController {

	Public Reservation__c resObj {get;set;}
	public ReservationController() {
	 resObj = new Reservation__c()
	}
	
	public void save() {
	 try {
	  insert resObj;
	 }
	 
	 catch(Exception ex) {
	 
	  system.debug('Exception is''+ex);
	 }
	
	}

}

You did initialize your resObj under a method which was not called at all, should be initialzed under contructor .

Marked solved if does work for you 

All Answers

Rahul.MishraRahul.Mishra
Hi Katie,

Update your controller with following code:
 
public with sharing class ReservationController {

	Public Reservation__c resObj {get;set;}
	public ReservationController() {
	 resObj = new Reservation__c()
	}
	
	public void save() {
	 try {
	  insert resObj;
	 }
	 
	 catch(Exception ex) {
	 
	  system.debug('Exception is''+ex);
	 }
	
	}

}

You did initialize your resObj under a method which was not called at all, should be initialzed under contructor .

Marked solved if does work for you 
This was selected as the best answer
Katie KourtakisKatie Kourtakis
Thanks! That worked.