1

Error comes at line 23. Line 23 is mentioned below as a code comment. I also tried by commenting this line and still error is there. But at line 24 which is upsert(Event);

Controller

public with sharing class EventsPageController {
    public Event__c Event {get; set;}
    public String datesFromCalendar {get; set;}
    public List<String> eachDt = new List<String>();

    public void EventsPageController() {
        Event = [SELECT Id, EventDates__c,Event_Description__c,Event_Type__c,Maximum_Attendees__c,Occurrence__c,Program_Name__c FROM Event__c WHERE Id = :ApexPages.currentPage().getParameters().get('id')];

    }

    public Event__c getEvent(){
        return Event;
    }

    public PageReference saveData(){
        System.debug(datesFromCalendar);
     /*   eachDt = datesFromCalendar.split(',');
        for(Integer i=0; i<eachDt.size(); i++){
            System.debug('eachDt contents : '+eachDt[i]);
        }*/

        try {
            Event.EventDates__c = datesFromCalendar;//LINE 23
            upsert(Event);
        } catch(System.DMLException e) {
            ApexPages.addMessages(e);
            return null;
        }
        // After Save, navigate to the default view page:
        return (new ApexPages.StandardController(Event)).view(); 

    }
}

Visualforce

<apex:form >
            <apex:pageMessages id="errors" />
            <apex:pageBlock title="Event Scheduler" mode="edit">                
                <apex:pageBlock title="Event Detail" mode="edit" id="criteria">

                    Event Type
                    <apex:inputField id="eventType" value="{!Event.Event_Type__c}" required="true" />
                    <label for="event_start_date">Start on/after</label>
                    <input type="text" name="event_start_date" id="from" placeholder="On or after" style="height:20px; width:200px;" />
                    <label for="event_end_date">End on/before</label>
                    <input type="text" name="event_end_date" id="to" placeholder="On or before" style="height:20px; width:200px;" />
                    <label for="occurrence">Occurrence</label> 
                    <select name="occurrence" id="occurrence">
                        <option >Select Occurrence</option>
                        <option value="0">One Time</option>
                        <option value="7">Weekly</option>
                        <option value="30">Monthly</option>
                        <option value="15">Bi-Monthly</option>
                    </select>

                    <label>Event Description</label>
                    <apex:inputField id="eventDescription" value="{!Event.Event_Description__c}"  />

                    <label>Maximum Attendees</label>
                        <apex:inputField id="maxAttendees" value="{!Event.Maximum_Attendees__c}" required="true" />

                    <label>Program Name</label>
                        <apex:inputField id="progName" value="{!Event.Program_Name__c}" required="true" />

                    </apex:pageBlock>
                <apex:pageBlock title="Calendar" id="calendarBlock">
                    <div align="right">
                        <select id="selectedDates" style="visibility:hidden" ></select>
                        <apex:inputHidden value="{!datesFromCalendar}" id="hiddenDatesCollector"/>
                        <apex:actionFunction name="passDatesToController" action="{!saveData}" rerender="hiddenDatesCollector"/>
                    </div>
                    <div>
                        <apex:commandButton id="saveEventBtn" value="Save Event" onclick="sendSelectedDates();" action="{!saveData}" />

                    </div>
                    <div id="myCalendar"></div> 
                </apex:pageBlock>
            </apex:pageBlock>
        </apex:form>
6
  • When you access your page are you providing the Id parameter (e.g. /yourpagename?id=YOURIDHERE and is it a valid Event__c Id? Commented Jan 16, 2015 at 11:18
  • your Event property is null
    – Novarg
    Commented Jan 16, 2015 at 11:19
  • @Novarg There is no record exists in this object yet. This will be the first record, if get inserted.
    – Superman
    Commented Jan 16, 2015 at 11:58
  • @AlexTennant There is no record exists in this object yet. This will be the first record, if get inserted.
    – Superman
    Commented Jan 16, 2015 at 11:59
  • 1
    Based on your replies to BarCotter below you're probably better off using the standard controller for Event__c and then writing a small function which you use instead of the standard save action to populate the date and then call the standard controler's save() method to perform the upsert. That approach would remove a lot of the manual null checking and querying you're going to have to with your current approach. Commented Jan 16, 2015 at 12:07

3 Answers 3

2

Why this code failed in creating a new record? If you look at the error it is saying "Attempt to De-reference a null object". And the error was at line Event.EventDates__c = datesFromCalendar; What is happening? Instance Event of the Event__c object trying to set the value of a field EventDates__c. And this object isn't exists actually at first.

Before going to make an attempt to set a value, insert or upsert an object. It should have to be instantiated. And we'll do it this way.

 public Event__c Event{ 
     get {
      if (Event == null)
        Event = new Event__c();
      return Event;
    }
    set;
  }

Final comments: Event object was null and I wasn't instantiating it before going to utilize it. Hope it will help you. :)

0

As @Novarg mentioned. Event is null. What is most likely happening is that the Id parameter is not being passed to the page correctly, the page should be referenced something like: /EventsPage?id=1234567890

If you are using the correct URL then make sure that the Id is correctly being returned by this bit of code:

ApexPages.currentPage().getParameters().get('id')

Once you know the Id then make sure that the SOQL below is actually returning 1 event record:

Event = [SELECT Id, EventDates__c,Event_Description__c,Event_Type__c,Maximum_Attendees__c,Occurrence__c,Program_Name__c FROM Event__c WHERE Id = :ApexPages.currentPage().getParameters().get('id')];
8
  • One thing I should have to mention in the question. That Event is a custom object newly created and haven't any record in the salesforce yet. Is this the problem that Event don't get initialized with Id?
    – Superman
    Commented Jan 16, 2015 at 11:35
  • Yes that would cause a problem. You need to have an Event Id to include in the URL.
    – BarCotter
    Commented Jan 16, 2015 at 11:37
  • Also my command button passing a JS value which get stored here Event.EventDates__c = datesFromCalendar; Is it ok to take two tasks from single button?
    – Superman
    Commented Jan 16, 2015 at 11:38
  • Or If I don't reference it anywhere and just try to save the record. Or I don't know any suggestion please how to handle such a case where data has to be saved for a newly created object.
    – Superman
    Commented Jan 16, 2015 at 11:41
  • I don't understand what you mean by "two tasks from a single button", but one issue I can see is that datesFromCalendar is a String so when you do Event.EventDates__c = datesFromCalendar; you will get an error. You will need to convert the String into a Date.
    – BarCotter
    Commented Jan 16, 2015 at 11:42
-1
    public void EventsPageController() {
 Event = new Event__c();
        Event = [SELECT Id, EventDates__c,Event_Description__c,Event_Type__c,Maximum_Attendees__c,Occurrence__c,Program_Name__c FROM Event__c WHERE Id = :ApexPages.currentPage().getParameters().get('id')];

    }

Initialize the Event object.

2
  • It is declared above at very first line of the class. Why do I need to recreate it in this method? public Event__c Event {get; set;}
    – Superman
    Commented Jan 16, 2015 at 13:27
  • Its not working this way. Null pointer error is not coming but it is redirecting back to the VF page with validation errors. Which is obviously because it is trying to send or resend with blank values.
    – Superman
    Commented Jan 16, 2015 at 13:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .