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
RahulRahul 

Hello Friends, I have a small issue in login Visualforce page. After entering username then date and clicking on login, its giving Null error. Its not querying date and Please have a look

When I eneter User name and Date in the input fields then click on login button , it should check whether that User name which Iam referring to last name in lead and that date of birth  is present in the Lead. If its present then it should Login and redirect to a visualforce page. Its working fine if iam doing without date when when Iam querying date its throwing the Null pointer exceptio, Attempt to reference null object error. I think its error in the query.Date should be converted in hh:mm:ss i guess please help.

Please  find the code.

public with sharing class LoginCntrl {
    public string userName{get;set;}
   public Lead Dateofbirth{get;set;}
   
  
    public pageReference doLogin() {
         
        if(String.isBlank(userName)) return null;  
        
        string dateformat;
        
        
        
              
        List<Lead> conls = [select id, lastname from Lead where LastName = :userName and date_of_birth__c=:Dateofbirth.Date_of_Birth__c];
        system.debug('dddddddddddddddddd'+conls );
        if(conls.size() == 1 ) {
            return new pageReference('/apex/studentpersonaldetails');
        }
        else {
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Please Enter correct User Name or Date of Birth'));

        userName = null;
        Dateofbirth = null;
            return null;
        
        }
        
    }

}

VF :-

<apex:page controller="LoginCntrl" showHeader="false" sidebar="false">
    <apex:form >
        <apex:image id="theImage" value="{!$Resource.nmims2}" width="350" height="60"/><br/>

    <apex:pageBlock title="Login Page" >
    <apex:pageMessages ></apex:pageMessages>

    <br/>

      User Name :  <apex:inputtext value="{!userName}"/> <br/>
       <br/>Date of birth: <apex:inputfield value="{!Dateofbirth.Date_of_Birth__c}"/><br/>
       <br/>
    
        <apex:commandButton style="margin-left:10px" value="Login" action="{!doLogin}"/>
</apex:pageBlock>
    </apex:form>
</apex:page>

 
Best Answer chosen by Rahul
Raj VakatiRaj Vakati
Change is as below 
 
<apex:page controller="LoginCntrl" showHeader="false" sidebar="false">
    <apex:form >
        <apex:image id="theImage" value="{!$Resource.nmims2}" width="350" height="60"/><br/>

    <apex:pageBlock title="Login Page" >
    <apex:pageMessages ></apex:pageMessages>

    <br/>

      User Name :  <apex:inputtext value="{!userName}"/> <br/>
       <br/>Date of birth: <apex:input type="date" value="{!Dateofbirth}"/><br/>
       <br/>
    
        <apex:commandButton style="margin-left:10px" value="Login" action="{!doLogin}"/>
</apex:pageBlock>
    </apex:form>
</apex:page>
 
public with sharing class LoginCntrl {
   public string userName{get;set;}
   public Date Dateofbirth{get;set;}
    public pageReference doLogin() {
      if(String.isBlank(userName)) 
		  return null;  
	  Date dob =null ; 
	  List<Lead> conls =null ; 
	  if(Dateofbirth ==null){
		  //dob = System.today();
		 conls =  [select id, lastname from Lead where LastName = :userName ]  ; 
	  }else{
		  conls = [select id, lastname from Lead where LastName = :userName AND  date_of_birth__c=:Dateofbirth ] ; 
	  }
        if(conls.size() == 1 && conls!=null) {
            return new pageReference('/apex/studentpersonaldetails');
        }
        else {
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Please Enter correct User Name or Date of Birth'));
			userName = null;
			Dateofbirth = null;
            return null;
        }
    }

}

 

All Answers

Raj VakatiRaj Vakati
Change your code like below 
 
public with sharing class LoginCntrl {
   public string userName{get;set;}
   public Lead Dateofbirth{get;set;}
    public pageReference doLogin() {
      if(String.isBlank(userName)) 
		  return null;  
	  Date dob =null ; 
	  if(Dateofbirth.Date_of_Birth__c ==null){
		  dob = System.today();
	  }else{
		  dob = Dateofbirth.Date_of_Birth__c ; 
	  }
		List<Lead> conls = [select id, lastname from Lead where LastName = :userName AND  date_of_birth__c=:dob];
        if(conls.size() == 1 && conls!=null) {
            return new pageReference('/apex/studentpersonaldetails');
        }
        else {
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Please Enter correct User Name or Date of Birth'));
			userName = null;
			Dateofbirth = null;
            return null;
        }
    }

}

 
Raj VakatiRaj Vakati
Or you can do it like this also
public with sharing class LoginCntrl {
   public string userName{get;set;}
   public Lead Dateofbirth{get;set;}
    public pageReference doLogin() {
      if(String.isBlank(userName)) 
		  return null;  
	  Date dob =null ; 
	  List<Lead> conls =null ; 
	  if(Dateofbirth.Date_of_Birth__c ==null){
		  //dob = System.today();
		 conls =  [select id, lastname from Lead where LastName = :userName ]  ; 
	  }else{
		  conls = [select id, lastname from Lead where LastName = :userName AND  date_of_birth__c=:Dateofbirth.Date_of_Birth__c ] ; 
	  }
        if(conls.size() == 1 && conls!=null) {
            return new pageReference('/apex/studentpersonaldetails');
        }
        else {
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Please Enter correct User Name or Date of Birth'));
			userName = null;
			Dateofbirth = null;
            return null;
        }
    }

}

 
RahulRahul
Hi Raj, Thanks for the Reply. I am still getting the same error.

Visualforce Error
System.NullPointerException: Attempt to de-reference a null object
Error is in expression '{!doLogin}' in component <apex:commandButton> in page loginpage: Class.LoginCntrl.doLogin: line 9, column 1
Class.LoginCntrl.doLogin: line 9, column 1
Raj VakatiRaj Vakati
try this
 
public with sharing class LoginCntrl {
   public string userName{get;set;}
   public Lead Dateofbirth{get;set;}
    public pageReference doLogin() {
      if(String.isBlank(userName)) 
		  return null;  
	  Date dob =null ; 
	  List<Lead> conls =null ;
if(Dateofbirth!=null){	  
	  if(Dateofbirth!=null && Dateofbirth.Date_of_Birth__c ==null){
		  //dob = System.today();
		 conls =  [select id, lastname from Lead where LastName = :userName ]  ; 
	  }
}else{
		  conls = [select id, lastname from Lead where LastName = :userName AND  date_of_birth__c=:Dateofbirth.Date_of_Birth__c ] ; 
	  }
        if(conls.size() == 1 && conls!=null) {
            return new pageReference('/apex/studentpersonaldetails');
        }
        else {
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Please Enter correct User Name or Date of Birth'));
			userName = null;
			Dateofbirth = null;
            return null;
        }
    }

}

 
Raj VakatiRaj Vakati
try this
public with sharing class LoginCntrl {
   public string userName{get;set;}
   public Date Dateofbirth{get;set;}
    public pageReference doLogin() {
      if(String.isBlank(userName)) 
		  return null;  
	  Date dob =null ; 
	  List<Lead> conls =null ; 
	  if(Dateofbirth ==null){
		  //dob = System.today();
		 conls =  [select id, lastname from Lead where LastName = :userName ]  ; 
	  }else{
		  conls = [select id, lastname from Lead where LastName = :userName AND  date_of_birth__c=:Dateofbirth ] ; 
	  }
        if(conls.size() == 1 && conls!=null) {
            return new pageReference('/apex/studentpersonaldetails');
        }
        else {
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Please Enter correct User Name or Date of Birth'));
			userName = null;
			Dateofbirth = null;
            return null;
        }
    }

}
 
<apex:page controller="LoginCntrl" showHeader="false" sidebar="false">
    <apex:form >
        <apex:image id="theImage" value="{!$Resource.nmims2}" width="350" height="60"/><br/>

    <apex:pageBlock title="Login Page" >
    <apex:pageMessages ></apex:pageMessages>

    <br/>

      User Name :  <apex:inputtext value="{!userName}"/> <br/>
       <br/>Date of birth: <apex:inputfield value="{!Dateofbirth}"/><br/>
       <br/>
    
        <apex:commandButton style="margin-left:10px" value="Login" action="{!doLogin}"/>
</apex:pageBlock>
    </apex:form>
</apex:page>

 
RahulRahul
Hi raj, Iam getting this Error

 Could not resolve the entity from <apex:inputField> value binding '{!Dateofbirth}'. <apex:inputField> can only be used with SObjects, or objects that are Visualforce field component resolvable.
 
RahulRahul
I dont know why its not searching for entered date of birth from page to the lead. 
Raj VakatiRaj Vakati
Change is as below 
 
<apex:page controller="LoginCntrl" showHeader="false" sidebar="false">
    <apex:form >
        <apex:image id="theImage" value="{!$Resource.nmims2}" width="350" height="60"/><br/>

    <apex:pageBlock title="Login Page" >
    <apex:pageMessages ></apex:pageMessages>

    <br/>

      User Name :  <apex:inputtext value="{!userName}"/> <br/>
       <br/>Date of birth: <apex:input type="date" value="{!Dateofbirth}"/><br/>
       <br/>
    
        <apex:commandButton style="margin-left:10px" value="Login" action="{!doLogin}"/>
</apex:pageBlock>
    </apex:form>
</apex:page>
 
public with sharing class LoginCntrl {
   public string userName{get;set;}
   public Date Dateofbirth{get;set;}
    public pageReference doLogin() {
      if(String.isBlank(userName)) 
		  return null;  
	  Date dob =null ; 
	  List<Lead> conls =null ; 
	  if(Dateofbirth ==null){
		  //dob = System.today();
		 conls =  [select id, lastname from Lead where LastName = :userName ]  ; 
	  }else{
		  conls = [select id, lastname from Lead where LastName = :userName AND  date_of_birth__c=:Dateofbirth ] ; 
	  }
        if(conls.size() == 1 && conls!=null) {
            return new pageReference('/apex/studentpersonaldetails');
        }
        else {
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Please Enter correct User Name or Date of Birth'));
			userName = null;
			Dateofbirth = null;
            return null;
        }
    }

}

 
This was selected as the best answer