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
Abhishek Singh 88Abhishek Singh 88 

getting error Attempt to de-reference a null object

Hi Developers,
I have writtent code where i am checking that object__c have any duplicate value in text area field.I have written bellow code
if( ( email.subject.containsIgnoreCase('Ticket#') || email.subject.containsIgnoreCase('Ticket #') || email.subject.containsIgnoreCase('Incident#') || email.subject.containsIgnoreCase('Incident #') ) && email.subject.contains('[')){
        ticketid = email.Subject.substring(email.Subject.indexOf('[')+1, email.Subject.indexOf(']'));
      }else if(email.subject.containsIgnoreCase('Ref:IN:')){
        ticketid = email.subject.substring(email.Subject.indexOf('IN:')+3,email.Subject.indexOf(')'));
      }
      List<BMCServiceDesk__Incident__c> newinci=new List<BMCServiceDesk__Incident__c>();
      system.debug('trying to insert ticket values');
      for(BMCServiceDesk__Incident__c inci:[select id,BMCServiceDesk__EmailServiceAddress__c from BMCServiceDesk__Incident__c])
      {
          if (inci.BMCServiceDesk__EmailServiceAddress__c.contains(ticketid))
          {
              newinci.add(inci);
              system.debug('ticket id added to incident');
          }
      }
it showing  Attempt to de-reference a null object error 
for this one
for(BMCServiceDesk__Incident__c inci:[select id,BMCServiceDesk__EmailServiceAddress__c from BMCServiceDesk__Incident__c])
      {
          if (inci.BMCServiceDesk__EmailServiceAddress__c.contains(ticketid))
          {
              newinci.add(inci);
              system.debug('ticket id added to incident');
          }
}

Any help will be appriciated.
Best Answer chosen by Abhishek Singh 88
LBKLBK
Use this code.
 
for(BMCServiceDesk__Incident__c inci:[select id,BMCServiceDesk__EmailServiceAddress__c from BMCServiceDesk__Incident__c])
      {
          if(inci.BMCServiceDesk__EmailServiceAddress__c != null && ticketid != null){
			  if (inci.BMCServiceDesk__EmailServiceAddress__c.contains(ticketid))
			  {
				  newinci.add(inci);
				  system.debug('ticket id added to incident');
			  }
		  }
}

 

All Answers

LBKLBK
Can you look for the possibility of ticketid being null?

Where have you declared it?
Hemant_JainHemant_Jain
Please give the exact line number to analyse the issue. 

At first, in the block the possiblity is ticketid is null. Kindly initialize it to a blank value if not done already.
PrasathPrasath
try this

for(BMCServiceDesk__Incident__c inci:[select id,BMCServiceDesk__EmailServiceAddress__c from BMCServiceDesk__Incident__c])
      {
          if (inci.BMCServiceDesk__EmailServiceAddress__c != null && inci.BMCServiceDesk__EmailServiceAddress__c.contains(ticketid))
          {
              newinci.add(inci);
              system.debug('ticket id added to incident');
          }
}
Abhishek Singh 88Abhishek Singh 88
Hi Hemant.
it is showing error on line 10 column 1.
Abhishek Singh 88Abhishek Singh 88
Hi Prasath,
Thanks for your sugession.
I tried but still it giving same error.
If i remove if statement then it is working fine
PrasathPrasath
Either one of the "inci.BMCServiceDesk__EmailServiceAddress__c" or "ticketid" values are null thats why showing that error verify both have values. 
Abhishek Singh 88Abhishek Singh 88
I have declare ticketId as
String ticketId='';
LBKLBK
Use this code.
 
for(BMCServiceDesk__Incident__c inci:[select id,BMCServiceDesk__EmailServiceAddress__c from BMCServiceDesk__Incident__c])
      {
          if(inci.BMCServiceDesk__EmailServiceAddress__c != null && ticketid != null){
			  if (inci.BMCServiceDesk__EmailServiceAddress__c.contains(ticketid))
			  {
				  newinci.add(inci);
				  system.debug('ticket id added to incident');
			  }
		  }
}

 
This was selected as the best answer
Abhishek Singh 88Abhishek Singh 88
Now the error is gone But in debug log 
system.debug('ticket id added to incident');

this line is looping.,it not coming out from loop
LBKLBK
You have added it in the for loop, so how many ever records returned by the following SOQL query, it will loop that many times.
[select id,BMCServiceDesk__EmailServiceAddress__c from BMCServiceDesk__Incident__c]
Put a debug statement outside the for loop to observe when it comes out of the loop.

This kind of FOR loops cannot go on forever.

 
Abhishek Singh 88Abhishek Singh 88
Thanks LBK.