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
Sudhir NarayanswamySudhir Narayanswamy 

remove null value from list

Hi, 

 How to remove null values from list in apex

Thanks
Sudhir
Amit Chaudhary 8Amit Chaudhary 8
sample code for you
Integer j = 0;
while (j < ListAccount.size())
{
  if(ListAccount.get(j).Account_Number__c == null)
  {
    ListAccount.remove(j);
  }else
  {
    j++;
  }
}
Please check below post for all list method
1) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_list.htm

Let us know if this will help you
 
Malni Chandrasekaran 2Malni Chandrasekaran 2
Sudhir,
With the details you have given, I assume you have to remove list items from the list if any particular column value is null. If yes, please try the below code.

integer ind = 0 ;
while (ind < listname.size())
   {
       if ( listname.get(ind).columnname == null)
            {  listname.remove(ind);
            }
       else
           { ind++;
           }
}
//Underlined are variable names.
if this is not your requirement, please add more details.

Thanks.
Hazarath MHazarath M

HI

with Set there is an option to remove the null directly instea with index position. 
setOfRecords.remove(null) will remove all null records from the set.