1

I'm trying to debug someone else's code where they're trying to populate a visualforce picklist with options from the controller. Without going into detail, the page is failing to load because the constructor that populates the picklist is attempting to loop over a null list so we get the "Argument 1 cannot be null" message, which is to be expected.

However, I cannot recreate the issue in the sandbox? My debug statements clearly show that the list that is being iterated over is null but the page loads regardless.

Here's a snippet of the code:

public List<SelectOption> getPublicOptions() {
    List<Custom_Object__c> objs = [Select Id, Name FROM Custom_Object__c Where Id = :customVariable Order By Name];
    system.debug('-------objs List-------'+objs); // This is showing ()
    List<SelectOption> options = new List<SelectOption>();

    Debug log shows fatal error at this line-->for (Custom_Object__c co :objs){
          options.add(new SelectOption(co.id,co.Name));
    }

    return options;
}
  • 4
    A query like you've got into the List<Custom_Object__c> objs will never be null. It'll be an empty list - just as you demonstrate with ( ) from the debug. As written, your code would work just fine. objs will be an empty list and the for loop will simply have no work to do and return the empty options collection. What else is happening on the "fatal error" line in your actual code? (The error describes your code sending a null into the first parameter of some method where that is disallowed) – Mark Pond Apr 9 '16 at 22:44
  • Agreed with @Mark Pond. The issue may not be in the for 'header' but in the body of the loop itself. Could one of your custom objects have a null name? – mkorman Apr 10 '16 at 8:07
  • Thanks @MarkPond and mkorman, you're right, that shouldn't be producing an error since it's an empty list and wouldn't have anything to loop over. It's a strange situation because there were apparently no modifications made to the code and one day it just broke and started throwing the "Argument 1 cannot be null" message in production? – sfdcJameson Apr 10 '16 at 17:45
  • Could it be that you've simplified your code in the example? The exception is more likely to be thrown on the line below (where you add a new SelectOption to the list). If you've simplified the code for the question, could you post the entire snippet? – rael_kid Apr 11 '16 at 13:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.