I am checking in the Cases list in my trigger, for record type Id and only if case is of that particular Record Type do I want a SOQL query to run.

But since a list is empty it throws a Null Pointer error. How can I prevent this?

if(cases[0].RecordTypeId == 'RecordTypeId') {

    cases_Creative = [SELECT Id, CaseNumber,Counter_Survey__c FROM Case Where RecordTypeId ='RecordTypeId' and Counter_Survey__c != null and createdDate = Last_N_Days:10 order by Counter_Survey__c desc limit 1];

It sounds like you may need to bulkify your code, because multiple records could occur in a trigger. That said, to solve your immediate problem, you just need to use the && shortcircuit operator:

if(!cases.isEmpty() && cases[0].RecordTypeId == 'RecordTypeId') {
| improve this answer | |
  • Thanks for the reply SFDCFOX. But getting the same null Pointer error. – Abhishek Apr 12 '17 at 13:48
  • Did you define cases? Try adding System.debug(cases == null); before the if statement and see if it ends up being null in the debug logs. – battery.cord Apr 12 '17 at 14:14
  • @battery.cord: Debug says 'false' when I put system.debug before if statement with cases == null. thanks! – Abhishek Apr 12 '17 at 14:31
  • Hey @battery.cord any pointers here please. – Abhishek Apr 13 '17 at 8:44
  • 1
    @Abhishek Are you sure you're on the right line number, then? Maybe it's actually a completely different line? Assuming cases == null was false, then neither line is the problem. – sfdcfox Apr 13 '17 at 12:36

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.