Find your content:

Search form

You are here

Attempt to de-reference a null object


I am getting above error in following code

public List<phonesterDat> getCompanyData() {
  List<phonesterDat> var1 = new List<phonesterDat>();
  List<Account> accounts = [select DD_Segment__c from Account];
  List<Keyword__c> keywords = [select kw__c from Keyword__c];

  for (Keyword__c keyword : keywords) {
    phonesterDat var = new phonesterDat();
    String str = keyword.kw__c;
    Integer cnt = 0;
    for (Account acc : accounts) {
      if (acc.DD_Segment__c.contains(str)) {
        cnt = cnt+1;

  return var1;

Attribution to: Bhushan Lodha

Possible Suggestion/Solution #1

Looking at the code given, chances are you're getting a Keyword__c result from your query which has a null value in the field kw__c, and as such you'd get this error around here:

String str = keyword.kw__c;

That said, I can't be sure and really you need to let us know have you've tried, and you've not listed the definition of phonesterDat so the error could easily be in the setType() method and we'd be none the wiser.

I recommend using the debug tools available (even just logs) to see what's null and where.

Attribution to: Matt Lacey

Possible Suggestion/Solution #2

You could try and solve this by debugging. Enable Debug Logs for your user under Admin Setup -> Monitoring. You can use System.debug() in your apex code for output to the debug logs.

I support LaceySnr in his answer, you probably have a null value in the kw__c field of keyword.

Attribution to: krobbens

Possible Suggestion/Solution #3

(Converting comment to answer)

I think it maybe acc.DD_Segment__c, which is null and therefore .contains is blowing up.

Add a not null check before doing the .contains(). (Or alternatively Where DD_Segment__c!=NULL to the soql query)

That said, it should tell you the line number in the exception stack trace.

Attribution to: techtrekker
This content is remixed from stackoverflow or stackexchange. Please visit

My Block Status

My Block Content