You need to sign in to do that
Don't have an account?
PC
Attempt to de-reference a null object for soql query
hi,
I have a favourite button which fetches the items which has been ordered more than twice.
This is my controller
Public List<Menu__c> favmenu {get;set;}
favmenu = [select id, name,Category__c,Description__c,Image__c,ImageURL__c,Dish_Name__c,Price__c,
Special_Dish__c,Status__c,SubCategory__c,veg_non_veg__c,remarks__c from Menu__c where id IN : menuIdList];
if(favmenu.size()>0)
nofavourites = 'Choose from your favourites';
else
nofavourites='No favourites yet';
When i m testing for favmenu.size()>0 ..... it is working fine....but for the else part, when the soql query is not fetching any item, it is showing the error
Attempt to de-reference a null object
An unexpected error has occurred. Your development organization has been notified.
I have a favourite button which fetches the items which has been ordered more than twice.
This is my controller
Public List<Menu__c> favmenu {get;set;}
favmenu = [select id, name,Category__c,Description__c,Image__c,ImageURL__c,Dish_Name__c,Price__c,
Special_Dish__c,Status__c,SubCategory__c,veg_non_veg__c,remarks__c from Menu__c where id IN : menuIdList];
if(favmenu.size()>0)
nofavourites = 'Choose from your favourites';
else
nofavourites='No favourites yet';
When i m testing for favmenu.size()>0 ..... it is working fine....but for the else part, when the soql query is not fetching any item, it is showing the error
Attempt to de-reference a null object
An unexpected error has occurred. Your development organization has been notified.
Try initializing the list before the Soql query. Like this:
All Answers
This is the full controller code
I am just trying to print the novafourites string.
Try initializing the list before the Soql query. Like this:
If this if condition doesn't satisfy, then favmenu will not be queried. Which means this will remain null.
In next when you try to check if(favmenu.size()>0) => it will be evaluating as if(null.size() > 0) which will give you attempt to deference null.
Just above the if, initialize the favmenu to new List() :