0
@RestResource(urlMapping='/CollectionMaster/*')
global with sharing class CollectionMaster{

    @HttpPOST
    global static CollectionWrapper getCollectionMaster(String userName){ 
         list<Product__c> crList;
         list<Product__c> crList1;
         User ur=[select id,Category__c,Collection__c,Collection_Name__c,Region__c,UserRole.name from  user where Username=:userName];
 //(error line shown here)//        String[] arrTest = ur.Category__c.split('\\;');   
            if(ur.UserRole.name =='SR')
       {

          crList= [select id,Brand__c,Category__c,Collection__c,Collection_Name__c from Product__c];
          crList1= [select id,Brand__c,Category__c,Collection__c,Collection_Name__c from Product__c LIMIT 50000];
       }
      else if(ur.UserRole.name =='RSM'){
      crList= [select id,Brand__c,Category__c,Collection__c,Collection_Name__c  from Product__c LIMIT 50000];
       system.debug('***RSM***');
        } else if(ur.UserRole.name =='HOD' || ur.UserRole.name =='MD'){
           crList= [select id,Brand__c,Category__c,Collection__c,Collection_Name__c from Product__c LIMIT 50000];
           }
1
  • 1
    Welcome to SFSE! Please take some time to read How to Ask and take the tour. Once you have done so, make sure you include a clear description of the problem alongside any errors.
    – Eduard
    Jul 23 '18 at 10:47
3

Attempt to de reference error occurred because Category__c was null and you were splitting it.

To avoid such errors, always add a null check.

if(String.isNotBlank(ur.Category__c)){
    String[] arrTest =  ur.Category__c.split('\;');
}

String.isNotBlank() method will check for both null and blank values. So, always use it.

1
  • if ur is null then it should give an error in SOQL query line, so no need to check ur!=null Jul 23 '18 at 10:52
-1

Attempt to de reference a null object error comes into the picture whenever we are referencing a null object in our code.

In your case there might be 2 possibilities

  1. Either ur is null
  2. OR Category is null

To overcome this kind of issue it is a best practice to always put a null check. Like below

IF(ur != null && ur.Category__c != null){
   String[] arrTest = ur.Category__c.split('\;');
}

Note: - If you are using arrTest variable in your code then declare it outside of If statement like below

List<String> arrTest =  new List<String>();
IF(ur != null && ur.Category__c != null){
       arrTest = ur.Category__c.split('\;');
    }

And put a null check and size check before using arrTest variable.

IF(arrTest != null && arrTest.size() > 0){
  // do stuff here
}
2
  • This answer is close, but inaccurate in two places. 1) The ur variable will never be null in OP's given code. If the query returns 0 rows, and you try to store the result in a single SObject instance, you'll get a no rows available for assignment exception. 2) arrTest will never be null in your example code because you're initializing the list on the same line you declare it.
    – Derek F
    Jul 23 '18 at 12:57
  • Yeah. Thanks for correcting me but it's always best practice to check for the null and size while working with the List. And if we are working with the single record then also use the null check to keep the code working instead of breaking in future.
    – Amit Singh
    Jul 23 '18 at 13:42

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.