+ Start a Discussion
Hugo StoupeHugo Stoupe 

Need help getting a date passed from a visualforce page to my controller

Newbie here (sorry folks!); and I'm trying to get a date passed to my controller.  I created a dummy object (dummyTable) containing a date field so that I could use the InputText on my visual force page for the date field.  I did this so that the date picker would work.

The date I think is now passing to the controller, but I keep getting this error:

System.NullPointerException: Attempt to de-reference a null object
Error is in expression '{!fetch}' in component <apex:commandButton> in page testtereport: Class.selectAllSOQLExampleController.fetch: line 31, column 1
Class.selectAllSOQLExampleController.fetch: line 31, column 1

If I change my query to hard-code a date, I don't get the error.

Can anyone help? 

Below is the Visual Force Page; and Controller:
 
<apex:page controller="selectAllSOQLExampleController" tabStyle="Account" docType="HTML-5.0">
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockButtons >
               
               <b>Start Date: </b><apex:inputfield value="{!dum.dateOne__c}"/>
               
                <apex:commandButton value="Fetch Time Entries" action="{!fetch}"/>
            </apex:pageBlockButtons>
            <apex:pageBlockSection columns="1" title="Dynamic SOQL Query" collapsible="True">
                <apex:outputText value="{!query}">
                </apex:outputText>
            </apex:pageBlockSection>
 
            <apex:pageBlockSection title="Billable Docket Report" columns="1" collapsible="False">Test
                <apex:pageBlockTable value="{!TEList}" var="tec">
                    <apex:column width="30" value="{!tec.Functional_Area__c}"/>
                    <apex:column width="20" value="{!tec.Standard_Activities__c}"/>
                    <apex:column value="{!tec.Activity_Other__c}"/>
                    <apex:column width="10" value="{!tec.Project__c}"/>
                    <apex:column width="10" value="{!tec.Contract__c}"/>
                    <apex:column width="10" value="{!tec.Monday__c}"/>
                    <apex:column width="10" value="{!tec.Tuesday__c}"/>
                    <apex:column width="10" value="{!tec.Wednesday__c}"/>
                    <apex:column width="10" value="{!tec.Thursday__c}"/>
                    <apex:column width="10" value="{!tec.Friday__c}"/>
                    <apex:column width="10" value="{!tec.subtotal__c}"/>
                </apex:pageBlockTable>
 
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>
and the controller:

You can see on in the Query String, that the commented out query is working (with the hard-coded date); but the one with the dum.dateOne__c is not working.
public class selectAllSOQLExampleController {

   
    public dummyTable__c dum{get;set;} 
    public String startdt{get;set;}
    public List<Time_Card_Entry__c> TEList{get;set;}
    public String query{get;set;}
    public String startDate{get;set;}

 
 
 //   public dummyTable__c dum() {
 //    if(dum == null) dum = new dummyTable__c();
 //   return dum;
 //   }
    
    public PageReference fetch(){
        String SobjectApiName = 'Time_Card_Entry__c';
        Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
        Map<String, Schema.SObjectField> fieldMap = schemaMap.get(SobjectApiName).getDescribe().fields.getMap();
 
        String commaSepratedFields = '';
        for(String fieldName : fieldMap.keyset()){
            if(commaSepratedFields == null || commaSepratedFields == ''){
                commaSepratedFields = fieldName;
            }else{
                commaSepratedFields = commaSepratedFields + ', ' + fieldName;
            }
        }
 //       query = 'select ' + commaSepratedFields + ' from ' + SobjectApiName + ' Where Monday_Date__c >=' + '2015-01-14' + ' Limit 500';
        query = 'select ' + commaSepratedFields + ' from ' + SobjectApiName + ' Where Monday_Date__c >=' + dum.dateOne__c + ' Limit 500';
         
  
 
        TEList = Database.query(query);
 
        return null;
    }
}


 
Best Answer chosen by Hugo Stoupe
Balaji Chowdary GarapatiBalaji Chowdary Garapati
Try this controller:
 
public class selectAllSOQLExampleController {

   
    public dummyTable__c dum{get;set;} 
    public String startdt{get;set;}
    public List<Time_Card_Entry__c> TEList{get;set;}
    public String query{get;set;}
    public String startDate{get;set;}

 
 
 //   public dummyTable__c dum() {
 //    if(dum == null) dum = new dummyTable__c();
 //   return dum;
 //   }
    public selectAllSOQLExampleController(){

           dum=new dummyTable__c();//Just try to instantiate and see if it works!


     }

    public PageReference fetch(){
        String SobjectApiName = 'Time_Card_Entry__c';
        Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
        Map<String, Schema.SObjectField> fieldMap = schemaMap.get(SobjectApiName).getDescribe().fields.getMap();
 
        String commaSepratedFields = '';
        for(String fieldName : fieldMap.keyset()){
            if(commaSepratedFields == null || commaSepratedFields == ''){
                commaSepratedFields = fieldName;
            }else{
                commaSepratedFields = commaSepratedFields + ', ' + fieldName;
            }
        }
 //       query = 'select ' + commaSepratedFields + ' from ' + SobjectApiName + ' Where Monday_Date__c >=' + '2015-01-14' + ' Limit 500';
        query = 'select ' + commaSepratedFields + ' from ' + SobjectApiName + ' Where Monday_Date__c >=' + dum.dateOne__c + ' Limit 500';
         
  
 
        TEList = Database.query(query);
 
        return null;
    }
}

Thanks,

balaji
 

All Answers

KevinPKevinP
One or more of your variables within this statement is coming back null. I suspect it's the dum.dateOne__c

Put a system.debug('Dum.dateOne__C == ' + dum.dateOne__c); statement in to see what it's returning from the page. 

But the proximate issue you're going to face is that dum is never instantiated as an object. So setting it's dateOne__c field won't actually work. (you commented that bit out. 
Balaji Chowdary GarapatiBalaji Chowdary Garapati
Try this controller:
 
public class selectAllSOQLExampleController {

   
    public dummyTable__c dum{get;set;} 
    public String startdt{get;set;}
    public List<Time_Card_Entry__c> TEList{get;set;}
    public String query{get;set;}
    public String startDate{get;set;}

 
 
 //   public dummyTable__c dum() {
 //    if(dum == null) dum = new dummyTable__c();
 //   return dum;
 //   }
    public selectAllSOQLExampleController(){

           dum=new dummyTable__c();//Just try to instantiate and see if it works!


     }

    public PageReference fetch(){
        String SobjectApiName = 'Time_Card_Entry__c';
        Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
        Map<String, Schema.SObjectField> fieldMap = schemaMap.get(SobjectApiName).getDescribe().fields.getMap();
 
        String commaSepratedFields = '';
        for(String fieldName : fieldMap.keyset()){
            if(commaSepratedFields == null || commaSepratedFields == ''){
                commaSepratedFields = fieldName;
            }else{
                commaSepratedFields = commaSepratedFields + ', ' + fieldName;
            }
        }
 //       query = 'select ' + commaSepratedFields + ' from ' + SobjectApiName + ' Where Monday_Date__c >=' + '2015-01-14' + ' Limit 500';
        query = 'select ' + commaSepratedFields + ' from ' + SobjectApiName + ' Where Monday_Date__c >=' + dum.dateOne__c + ' Limit 500';
         
  
 
        TEList = Database.query(query);
 
        return null;
    }
}

Thanks,

balaji
 

This was selected as the best answer
Hugo StoupeHugo Stoupe
Thanks - I'll give this a try!!

 
Hugo StoupeHugo Stoupe
I can't thank you both enough.  I had tried this and other options - maybe I had something mistyped or misplaced, but this one works!!