+ Start a Discussion
unidhaunidha 

Checking the null value

Hi ,

 

I know this is quite naive question but when I went through all the posted solution still I am not understand  the concept very well.

Here is my snippet of my code:

 

System.debug('check variable =' + qtr + segment + accId ); 

List<MyAccount__c>  lstReport=[Select id , Segment__c,(Select id,Name__c from  Account_Competitor__r)from MyAccount__c  where Fiscal_Quarter__c=:qtr    and Segment__c in :segment and MyAccount__c=:accId];

if(lstReport==null){
            System.debug('if null should return here'); 
          	lstReport=new List<MyAccount__c> ();
           
}

 

 

 

I already run the query in the Force.com console and yes it return no data with the printed variable in System.debug('check variable =' + qtr + segment + accId );  .

 

So I expect it should be null, as it also throw me Attempt to de-reference a null.

 

But it didn't enter the condition if(lstReport==null). So what I should do to check it is  return null value? 

 

Thanks in advance

unidha

Thiyagarajan.SelvarajThiyagarajan.Selvaraj

 

Hi 

 

Try this : if(lstReport.size() == 0 )

      insread of   if(lstReport == null )

Jerun JoseJerun Jose
SOQL queries will always return a list of records. If there are no records found then it will return an empty list, but a list nonetheless.

The difference is an empty list still has a memory spot allocated for it and so you can perform operations on it.
Something like lstReport.add or lstReport.size

With null, you dont have any memory tied to it, so no operations can be done.
null.add or null.size will throw an exception of attempting to dereference a null object.
unidhaunidha

Hi Thiyagarajan.Selvaraj and Jerun Jose,

 

I tried lstReport.size()==0 and lstReport.isEmpty() , both are not working. So how I am going to check whether the list is retuning no data.

 

 

Thanks,

unidha

Jerun JoseJerun Jose
You can inspect the debug logs and see how many rows are being returned.
unidhaunidha
Yes.I run the query in the workbench.No data return.I wonder how we can handle the situation in case no data return.


Sent from Samsung Mobile