I get this error if LargestRevision returns null. ie., fields revision and quote link have no values. Why isn't the else part handling null values? Following is the code I tried.

   LargestRevision=[SELECT Revision_Number__c FROM OPP_QuoteLink__c WHERE Quote_Link__c=:quoteLink.Quote_Link__c ORDER BY Revision_Number__c DESC LIMIT 1];      
          clonedQuteLink.Quote_Link__c = quoteLink.Quote_Link__c;                
          clonedQuteLink.Quote_Link__c = quoteLink.Id;                


Revision_Number__c is a number field.

Quote_Link__c is a lookup field

2 Answers 2


LargestRevision.size() will never return null.

The size() method will return 0 if the list is empty.

From the documentation:

Returns the number of elements in the list.

public Integer size()

Return Value
Type: Integer


List<Integer> myList = new List<Integer>();
Integer size = myList.size();
system.assertEquals(size, 0);

List<Integer> myList2 = new Integer[6];
Integer size2 = myList2.size();
system.assertEquals(size2, 6);

Changing to the following should resolve this:

if(LargestRevision.size() > 0) {
    clonedQuteLink.Quote_Link__c = quoteLink.Quote_Link__c;                
  • I get Attempt to de-reference a null object as ERROR
    – MnZ
    Jun 16, 2014 at 8:40
  • @MnZ What type is LargestRevision? If it's a List then that should not be possible, if it's an SObject you should be getting no rows to assign to SObject as your error. Are you sure you are getting that error on the lines you've posted above? Jun 16, 2014 at 8:52
  • LargestRevision is a list.
    – MnZ
    Jun 16, 2014 at 8:58
  • @MnZ Can you add the full error you're getting and the line it refers to in your question? Based on the information you've given so far there is no part of your code that should give you that error (assuming you've made the changes I've suggested correctly). Jun 16, 2014 at 9:01
  • ERROR is List index out of bounds: 0 Error is in expression for the line: clonedQuteLink.Revision_Number__c=LargestRevision.get(0).Revision_Number__c+1;
    – MnZ
    Jun 16, 2014 at 15:36

You should replace the first part of the if with

 if(LargestRevision!=null and LargestRevision.size()!=0){
      clonedQuteLink.Quote_Link__c = quoteLink.Quote_Link__c;                

because if LargestRevision is null (no values found) then you cannot call .size() on a null

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .