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
RajashriRajashri 

Attempting to de-reference null object

Hi,

 

I am getting an error attemptiong to de-reference null object.

 

Can anyone please guide?

 

List<Sub_Proj__c> approvals = [select Id  FROM
                      Sub_Proj__c WHERE ID = :projectDetail.Id];

       if(approvals.isEmpty())
       {
          ApexPages.Message myMsg = new ApexPages.Message(ApexPages.
Severity.ERROR,'Approvals are not present');
                                                    
            ApexPages.addMessage(myMsg);
            return null;
       }
       else
       {
                 if(Projectdeatil.name!=null){
         // do rest work
           }
      
        }
   return returnPage;
    }

Best Answer chosen by Admin (Salesforce Developers) 
Noam.dganiNoam.dgani

Hi again

 

Anil - that will work, but isnt it a little wasteful on your mem allocation?

 

public PageReference editprj() { 
      PageReference returnPage = null;
	  List<Sub_Proj__c> appr;
	  //You should ask this question becuase if Detail is null, obviously you wont get anything back
	  //from the query
	  if(Detail != null)
	  {
			//Use a try-catch, because even if Detail is not null, there might be nothing to return.
			//Unless Detail is an instance of Sub_Proj__c
			try{
				appr = [select Id,Name  FROM
                      Sub_Proj___c WHERE ID = :Detail.Id];
			}
			catch(Exception e)
			{
				//Do your "nothing returned from query" logic - you might want to exit the method here too.
				returnPage = Something;
				return returnPage;
			}
			//Rest of your logic here - in case appr is not empty
	  }
	  else
	  {
			//Do your "nothing returned from query" logic - you might want to exit the method here too.
			returnPage = Something;
			return returnPage;
	  }

			return returnPage;
    }

 

 

 

All Answers

jd123jd123

Hi Rajashri.

 

List<Sub_Proj__c> approvals = [select Id  FROM
                      Sub_Proj__c WHERE ID = :projectDetail.Id];

       if(approvals.isEmpty())
       {
          ApexPages.Message myMsg = new ApexPages.Message(ApexPages.
Severity.ERROR,'Approvals are not present');
                                                    
            ApexPages.addMessage(myMsg);
            return null;
       }
       else
       {
                 if(Projectdeatil.name!=null){
         // do rest work
           }
      
        }
   return returnPage;
    }

 

why you are writing two return statements with in a single method,  if you are writing it will give comiple time error only i.e it won't save.

Noam.dganiNoam.dgani

Hi Rajashri

 

projectDetail is probably null and you are trying to refference ias ID.

 

regarding the return statement - Mahi that is not true, because it is inside an IF statement. He might want to exit the meet hod in case he hits that IF.

RajashriRajashri

Thanks for the reply!!

Below is my code.

 

public PageReference editprj() { 
      PageReference returnPage = null;
     List<Sub_Proj__c> appr = [select Id,Name  FROM
                      Sub_Proj___c WHERE ID = :Detail.Id];

       if(appr.isEmpty())
       {
          ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Approvals are not present');
                                                     
            ApexPages.addMessage(myMsg);
            return null;
       }
       else
       {
       
          do rest code here
        }
       
      return returnPage;
    }

Noam.dganiNoam.dgani

And your getting an error on 

   List<Sub_Proj__c> appr = [select Id,Name  FROM

                      Sub_Proj___c WHERE ID = :Detail.Id];

 

Than "Detail" is probably null. You should check if its null before referencing its ID, and decide what you want to do if its null.

 

i'm not sure what it is because its not a local variable of the method.

jd123jd123

Sorry, I didn't' see properly, I thought that in else also having the return statement

So the last line is won't be reachable.

Any way thanks for your post

sfdcconsultant2010sfdcconsultant2010

Attempting to de-reference null object error comes when the object is null / not initialized. in this case it seems Detail variable is null.

Anil MeghnathiAnil Meghnathi

ublic PageReference editprj()


     PageReference returnPage = null;

 

     List<Sub_Proj__c> appr=new  List<Sub_Proj__c>();
     appr = [select Id,Name  FROM Sub_Proj___c WHERE ID = :Detail.Id];

 

       if(appr.isEmpty())
       {
          ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Approvals are not present');                                       
          ApexPages.addMessage(myMsg);
          return null;
       }
       else
       {
       
          do rest code here
        }
       
      return returnPage;
    }

 

IF it solve your issue mark it as solution

Thanks

Noam.dganiNoam.dgani

Hi again

 

Anil - that will work, but isnt it a little wasteful on your mem allocation?

 

public PageReference editprj() { 
      PageReference returnPage = null;
	  List<Sub_Proj__c> appr;
	  //You should ask this question becuase if Detail is null, obviously you wont get anything back
	  //from the query
	  if(Detail != null)
	  {
			//Use a try-catch, because even if Detail is not null, there might be nothing to return.
			//Unless Detail is an instance of Sub_Proj__c
			try{
				appr = [select Id,Name  FROM
                      Sub_Proj___c WHERE ID = :Detail.Id];
			}
			catch(Exception e)
			{
				//Do your "nothing returned from query" logic - you might want to exit the method here too.
				returnPage = Something;
				return returnPage;
			}
			//Rest of your logic here - in case appr is not empty
	  }
	  else
	  {
			//Do your "nothing returned from query" logic - you might want to exit the method here too.
			returnPage = Something;
			return returnPage;
	  }

			return returnPage;
    }

 

 

 

This was selected as the best answer
Anil MeghnathiAnil Meghnathi

yes,obviously

we have to first check Detail sobject that whether it is null or not.if it is not null than we can use Detail.ID

 

Thanks

 

RajashriRajashri

Hey,Thank you guys..My Object reference was wrong and i corrected it, it  works fine now....