You need to sign in to do that
Don't have an account?
unidha
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
Hi
Try this : if(lstReport.size() == 0 )
insread of if(lstReport == null )
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.
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
Sent from Samsung Mobile