+ Start a Discussion
el mougali rachid 5el mougali rachid 5 

NullPointer error message at deploy from sandbox to production


I just use the right function to cut a string as is shown in the code below, the modified line is bold, the program works very well in the sand box, but when I deploy on the production environment I get a NullPointer error message on the modified line.

Thank you for your help

public without sharing class Ctrl001_NouveauProspect {
    public string keyword {set;get;}
    public string firstName{set;get;}
    public string lastName{set;get;}
    public string phone{set;get;}
    public string email{set;get;}
    public string CIN{set;get;}
    public boolean AddButton {set;get;}
    public boolean SearchResultButton{set;get;} 
    public boolean SearchResultLeadsButton{set;get;} 
    public boolean SearchResultAccountsButton{set;get;} 
    public boolean creationbutton{set;get;}     
    public Account [] accounts{set;get;}
    public List<Account> ResultListAccount{set;get;}
    public List<Lead> ResultListLead{set;get;}
    public Lead[] leads{set;get;}
    String accountQureyString ;
    String LeadQureyString ;

    public Account acc {set;get;}
    public Lead piste {set;get;}
    public Id accId {set;get;}
    public Id pisteId {set;get;}
    public Ctrl001_NouveauProspect(ApexPages.StandardController controller) {
        ResultListAccount = new List<Account>();
        ResultListLead = new List<Lead>();

    public Ctrl001_NouveauProspect() {
    AddButton = false;
    SearchResultButton =false;
    SearchResultLeadsButton =false;
    public PageReference createProspect() {

        string url = '/001/e?retURL=%2F001%2Fo';

        if(firstName != null) {
                  url  = url + '&name_firstacc2=' + firstName ;
        if(lastName != null) {
                  url  = url + '&name_lastacc2=' + lastName ;
        if(phone != null) {
                  url  = url + '&acc10=' + phone ;
        if(email != null) {
                  url  = url + '&PersonEmail=' + email ;
        if(CIN != null) {
                  url  = url + '&00N24000009s6qi=' + CIN ;
        return new Pagereference(url);
    public PageReference search(){
        AddButton = false;
        ResultListAccount = new List<Account>();
        ResultListLead = new List<Lead>();
        string phone_like = '%'+phone.right(9)+'%';
    if(CIN != null && CIN.contains(' '))
    {Apexpages.addMessage(new Apexpages.Message(Apexpages.Severity.WARNING,'Merci de saisir un CIN sans espaces'));}
    //if(keyword!=null && keyword!=''){
   // List<List <sObject>> myQuery = search.query(SOSL_search_string);
   // String searchquery='FIND\''+firstName+' '+lastName+' '+phone+' '+CIN+' *\'IN ALL FIELDS RETURNING Account(id,FirstName,LastName,Phone,N_piece_cni__c), Lead(id,FirstName,LastName,Phone)'; 
                   // 'SELECT Id FROM MyCustomObject__c WHERE field1__c = :myVariable.field1__c';   
      accountQureyString ='SELECT a.firstName, a.lastName, a.phone,a.PersonEmail, a.CIN__c from Account a WHERE ID !=null ';
      if(firstName!=null && firstName!='')
      { accountQureyString+='AND (a.firstName =: firstName OR a.lastName =: firstName)'; }
      if(lastName!=null && lastName!='') 
      {accountQureyString+='AND (a.lastName =: lastName OR a.firstName =: lastName)';}
      if(phone!=null && phone!='')
      //{ accountQureyString+='AND (a.phone =: phone OR a.PersonMobilePhone=: phone ) ';}
      { accountQureyString+='AND (a.phone like :phone_like OR a.PersonMobilePhone like :phone_like ) ';}
      if(CIN!=null && CIN!='') 
      {accountQureyString+='AND a.CIN__c=: CIN ';}
      if(email!=null && email!='') 
       accountQureyString+='AND a.PersonEmail =: email ';}
       accountQureyString+=' limit 20';
    leadQureyString ='SELECT a.firstName, a.lastName, a.phone, a.Email from Lead a WHERE ID !=null ';
     if(firstName!=null && firstName!='')
      { leadQureyString+='AND (a.firstName =: firstName OR a.lastName =: firstName)'; }
      if(leadQureyString!=null && lastName!='') 
      {leadQureyString+='AND (a.lastName =: lastName OR a.firstName =: lastName)';}
      if(phone!=null && phone!='')
      { leadQureyString+='AND (a.phone =: phone OR a.MobilePhone =: phone OR a.Fax =: phone)';}
      if(email!=null && email!='') 
      {leadQureyString+='AND a.Email =: email ';}
      leadQureyString+=' AND IsConverted = false limit 50';
   //String searchquery='FIND\''+firstName+' '+lastName+' '+phone+' '+CIN+' *\'IN All Fields RETURNING Account(id,FirstName,LastName,Phone,CIN__c), Lead(id,FirstName,LastName,Phone)'; 
    if((firstName!=null && firstName!='') || (lastName!=null && lastName!='') || (phone!=null && phone!='') || (CIN!=null && CIN!='') || (email!=null && email!=''))
    {ResultListAccount = Database.query(accountQureyString);}
    if((firstName!=null && firstName!='') || (lastName!=null && lastName!='') || (phone!=null && phone!='') || (email!=null && email!=''))
    {ResultListLead = Database.query(leadQureyString);}
    //accounts = ((List<Account>)searchList[0]);
    //leads = ((List<Lead>)searchList[1]);
    if(ResultListAccount != null ||ResultListLead != null /*ResultListAccount.size()>0 ||ResultListLead.size()>0*/){
    if(ResultListAccount != null && (ResultListLead == null|| ResultListLead.size()==0)/*ResultListAccount.size()>0*/){
   if(ResultListLead != null /*ResultListLead.size()>0*/){
   if ((CIN!=null && CIN!='')||(phone!=null && phone!='')|| (email!=null && email!='')){
  //  }
   if(ResultListAccount.size() == 0 && ResultListLead.size() == 0)
    return null;
    public PageReference addtoaccountteam(){
     AccountTeamMember[] newmembers = new AccountTeamMember[]{};  //list of new team members to add
     AccountShare[] newShare = new AccountShare[]{};  //list of new shares to add
     User currentuser =  [select id,name,department from user where id=:UserInfo.getUserId() LIMIT 1];  //get the user running the trigger, anyone that changes the Account will added to the account team
     ID uid=currentuser.id;
     String dept=currentuser.department;

          AccountTeamMember Teammemberad=new AccountTeamMember();
          //Teammemberad.TeamMemberRole = dept;
          Teammemberad.TeamMemberRole = 'Commercial';

     Database.SaveResult[] lsr = Database.insert(newmembers,false);//insert any valid members then add their     entry if they were successfully added
     Integer newcnt=0;
     for(Database.SaveResult sr:lsr){
        Database.Error emsg =sr.getErrors()[0];
        system.debug('\n\nERROR ADDING TEAM MEMBER:'+emsg);
        newShare.add(new AccountShare(UserOrGroupId=newmembers[newcnt].UserId, AccountId=newmembers[newcnt].Accountid, AccountAccessLevel='Edit',OpportunityAccessLevel='Edit',CaseAccessLevel='Read'));
     Database.SaveResult[] lsr0 =Database.insert(newShare,false); //insert the new shares
     Integer newcnt0=0;
     for(Database.SaveResult sr0:lsr0){
         Database.Error emsg0=sr0.getErrors()[0];
         system.debug('\n\nERROR ADDING SHARING:'+newShare[newcnt0]+'::'+emsg0);
     return new PageReference('/'+accId);

User-added image
Best Answer chosen by el mougali rachid 5

Seems variable "phone" is null. Is same test class getting pass in your sandbox? If it is failing in sandbox too then you may add debug statment to see the value of variable "phone"
You may add a null check before using variable

string phone_like;
if (phone != null){
 phone_like = phone.right(9);

All Answers

James LoghryJames Loghry
Hi there,

When you have a code question, please use the code format button (< >) when posting any code.  It helps us read the code, along with line numbers.  Since you have the line number in the error, please tell us which line it is too.

I'm guessing that you're not setting up the data correctly in your unit test.  If you're following best practices and NOT using seeAllData for the unit test, then you should be able to recreate the error in your sandbox prior to deploying.  Try that first.  

Since you know the line number (starts at line 10 in your searchTest method, and goes to line 70 in your search method), you should be able to fix it pretty easily.
el mougali rachid 5el mougali rachid 5
thank you for your response, and sorry to have put the code directly on the page, as this is the first time I used the forum.

I know that the error is generated by calling the method RIGHT, phone.right (9), and sincerely I do not know why when validating it return an error NullPointer

Seems variable "phone" is null. Is same test class getting pass in your sandbox? If it is failing in sandbox too then you may add debug statment to see the value of variable "phone"
You may add a null check before using variable

string phone_like;
if (phone != null){
 phone_like = phone.right(9);
This was selected as the best answer
James LoghryJames Loghry
In your test class, when you instantiate your Ctrl001_NouveauProspect, make sure you are setting the phone variable prior to calling the search method.
el mougali rachid 5el mougali rachid 5
thank you Abhishek_DEO, its working now.