0

I have a map of contacts. Need to print substring of title. but it errors out, when title is null :

System.NullPointerException: Attempt to de-reference a null object

public class k {
    public Map<id,Contact> getcc(){
        Map<id,Contact> cc=new Map<id,Contact>([select Id,title,name from Contact ]);
        system.debug('++++++++++++'+cc.get('0039000001EkSEz').title.substring(0,1));
        return cc;
    }
}
  • what exactly is ur requirement ? why r u querying all the contacts, if u jus need to get the title of only one contact ? – Vamsi Krishna Mar 11 '15 at 11:08
  • actually this code is a small testing part of web-service call, where i have to form a requestor string, and then send it to http req body , with each contact details. here i'm debugging with one contact, where title is null, and hence error occurs. – user59759 Mar 11 '15 at 11:11
  • updated the code with system.debug('++++++++++++'+ If(isNotBlank(cc.get('0039000001EkSEz')) ){ cc.get('0039000001EkSEz').title.substring(0,1)); but it errors out : unexpected token: 'If' – user59759 Mar 11 '15 at 11:29
3

You are getting this error because title field of that contact is null. There are 2 options for you to solve it:

  1. Check if title is not null before calling substring method
  2. Surround this part with try-catch

In first case your code would look like:

public Map<id,Contact> getcc(){
    Map<id,Contact> cc=new Map<id,Contact>([select Id,title,name from Contact]);
    if (cc.get('0039000001EkSEz').title != null)
        System.debug('++++++++++++'+cc.get('0039000001EkSEz').title.substring(0,1));
    else
        System.debug('This contact has no title');
    return cc;
}

This will check whether title field of this contact is not null(has some value).

In second case your code would be:

public Map<id,Contact> getcc(){
    Map<id,Contact> cc=new Map<id,Contact>([select Id,title,name from Contact]);
    try {
        System.debug('++++++++++++'+cc.get('0039000001EkSEz').title.substring(0,1));
    } catch (Exception e) {
        System.debug('This contact has no title. Exception: ' + e + '\nAt line: ' + e.getLineNumber());
    }
    return cc;
}

This will try to print the substring of the title of your contact, but if the title is null, it will catch an Exception. In this case, code in your catch block will be executed.

I'd also recommend reading about Exception handling

4

I m still not clear on your overall requirement.. but based on your code, to resolve the error you have couple of options

query only the contact records which has values in the title field or

Map<id,Contact> cc=new Map<id,Contact>([select Id,title,name from Contact where title != null ]);

before applying the substring, check if title is not null

string titleStr = cc.get('0039000001EkSEz').title;
if(titleStr != null)
system.debug('++++++++++++'+titleStr.substring(0,1));

but i would suggest not to query the entire contacts.. use query filters to look for a subset of records based on your scenario.. and please don't hardcode any record id's in your code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.