You need to sign in to do that
Don't have an account?

Apex- Phone Contact List - Remove spaces and caracters
Hello
I have string List of contact phone number,and I want to delete spaces and all caracteres like +, (, ) from this list.
this is my apex code:
list <Contact> cont = [Select id, phone from contact];
string contphone;
string phone;
List<string> contphonelist = new List<String>();
List<string> contphonelistvide = new List<String>();
if (cont.size() != null)
{
for (integer i=0; i< cont.size(); i++)
{
contphone = cont[i].phone;
contphone = contphone.replaceAll( '\\s+', '');
contphonelist.add(contphone);
}
}
system.debug('---contphonelist--- ' +contphonelist);
if (contphonelist.size() != null)
{
for (integer j=0; j< contphonelist.size(); j++)
{
phone= contphonelist[j];
}}
I have error in my page : Attempt to de-reference a null object
Une erreur inattendue s'est produite. Votre organisation de développement a été notifiée.
Can someone help me.
I have string List of contact phone number,and I want to delete spaces and all caracteres like +, (, ) from this list.
this is my apex code:
list <Contact> cont = [Select id, phone from contact];
string contphone;
string phone;
List<string> contphonelist = new List<String>();
List<string> contphonelistvide = new List<String>();
if (cont.size() != null)
{
for (integer i=0; i< cont.size(); i++)
{
contphone = cont[i].phone;
contphone = contphone.replaceAll( '\\s+', '');
contphonelist.add(contphone);
}
}
system.debug('---contphonelist--- ' +contphonelist);
if (contphonelist.size() != null)
{
for (integer j=0; j< contphonelist.size(); j++)
{
phone= contphonelist[j];
}}
I have error in my page : Attempt to de-reference a null object
Une erreur inattendue s'est produite. Votre organisation de développement a été notifiée.
Can someone help me.
Before replace any character you need to check record has null value or not.
Use below code, you will not receive Attempt to de-reference a null object error.
Hope this will help you. Let me know if it helped or you need any more assistance.
Please mark this is as the solution if it solved your purpose.
Thanks,
Sohan Raj Gupta
// you can use in your code for best query practices. How can filter data in the query?
list <Contact> cont = new List<Contact>();
cont = [Select id, Phone from contact Where Phone!=Null];
---Solution of this Error---
This is a very common error message which occurs when your object/list is not initialized (allocated memory). So when say not initialized means following:
In order to use the non-primitive data type in the code, we have initialized the memory to those data types. For example:
I want to use the Contact object in my apex class, so I have allocated a memory to that object. So to do that I have to write:
Contact ContactObj = new Contact(); // allocating memory to the Contact.
Similarly in case of list:
list<Contact> contList = new list<Contact>(); // allocating memory to list of Contacts.
So in your code make sure you have allocated the memory to your non-primitive data types, else you will run into the same error.
(1)
String s1 = 'Hello Hel lo Hello ';
s1= s1.replaceAll( '\\s+', '');
System.debug('*********************' + s1);
WIth \\s+ it will find all spaces in the string and replace it with the empty string.
(2)
string str = ' Hello';
str = str.trim(); will gives you 'Hello'
(3)
String txt = ' How Are You Today?';
System.debug(txt.deleteWhitespace());
Please mark it as best Answer if you find it helpful.
Thank You
Ajay Dubedi