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
tsalbtsalb 

Null list handling | error checking

I'm not sure how to check if the checkedvendor or selectedvendor list is null. The visualforce page searches for vendors, then you tick a box and "send RFP" to each one.

 

I am getting the following error when calling the sendRFP method when you fill in the Primary County - but do NOT perform a search before calling sendRFP. If you perform the search then call sendRFP - it just finishes return orderURL (last line in sendRFP).

 

caused by: System.NullPointerException: Attempt to de-reference a null object

Class.ProposalExtension.sendRFP: line 316, column 29 External entry point

 

What i need to know is, is it possible to do error checking in the method something like.. if(vendorList.size() <1) - throw an error. I'm not sure where to insert that into this method.

 

Controller Method

    public PageReference sendRFP() {
    	//Create a list to store the selected Vendors
    	List<Contact> selectedVendor = new List<Contact>();
    	//Loop through the Vendor search results and get the checked records
//316 	for(checkedVendor cV : vendorList) {       
    		if(cV.checked == true) {
    			selectedVendor.add(cV.ven);
    		}
    	}
    	//Create a new RFP record for each selected Vendor
    	for(Contact sVen : selectedVendor){
    		Proposal__c p = new Proposal__c();
    		p.Vendor__c = sVen.Id;
    		p.Order__c = orderId;
    		insert p;
    	}
    	//Set Service Request Valuation Order Date
    	Order__c ord = [SELECT Id, Valuation_Order_Date__c FROM Order__c WHERE Id = :orderId];
    	if(ord.Valuation_Order_Date__c == NULL){
    		ord.Valuation_Order_Date__c = system.today();
    	}
    	update ord;
    	
    	//Return user to the Order page
    	PageReference orderURL = new PageReference('/' + orderId);
    	return orderURL;
    }

 

 

 

Visualforce (if needed)

<apex:page standardController="Proposal__c" extensions="ProposalExtension" tabStyle="Proposal__c" cache="false">
    <apex:sectionHeader title="New RFP"/>
    <apex:form id="searchForm">
        <apex:pageBlock title="Vendor Search" id="SearchBlock" mode="edit">          
            <apex:pageBlockButtons >
                <apex:commandButton value="Search" action="{!vendorSearch}" rerender="results" status="status" id="searchButton" />
                <apex:commandButton value="Send RFP" action="{!sendRFP1}" status="status" id="rfpButton" />
            </apex:pageBlockButtons>
            <apex:pageBlockSection title="Search Criteria" columns="2" collapsible="false" showHeader="false">
                <apex:inputField id="primaryCounty" value="{!con.Primary_County__c}" required="true"/>              
            </apex:pageBlockSection>
            <apex:pageBlockSection title="Search Results" id="results" columns="1" collapsible="false">
                <apex:actionStatus id="status" startText="searching..." />   
                <apex:pageBlockTable value="{!vendorList}" var="v" id="searchResults">
                    <apex:column >
                        <apex:facet name="header"> 
                            <apex:inputCheckbox onclick="checkAll(this);"/>
                        </apex:facet>
                    <apex:inputCheckbox value="{!v.Checked}" id="selectedVendor"/>
                    </apex:column>                
                    <apex:column headerValue="Vendor Name">
                        <apex:outputLink value="/{!v.ven.Id}">{!v.ven.Name}</apex:outputLink>
                    </apex:column> 
                     <apex:column headerValue="Account Name">
                        <apex:outputLink value="/{!v.ven.Account.Id}">{!v.ven.Account.Name}</apex:outputLink>
                    </apex:column>       
                </apex:pageBlockTable> 
            </apex:pageBlockSection>          
        </apex:pageBlock>      
    </apex:form>
<script>
    function checkAll(cb) {
        var inputElem = document.getElementsByTagName("input");
        for(var i=0; i<inputElem.length; i++) {
            if(inputElem[i].id.indexOf("selectedVendor")!=-1)
            inputElem[i].checked = cb.checked;
        }
    }   
</script>
</apex:page>

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Nisse Knudsen.ax1026Nisse Knudsen.ax1026

If your vendorList is null, then you can't even call the .size(), as the size of nothing is: null.

So you could take two approaches: 

 

Either you disable the SendRFP button while vendorList is null 

 

<apex:pageBlockButtons >
                <apex:commandButton value="Search" action="{!vendorSearch}" rerender="results" status="status" id="searchButton" />
                <apex:commandButton value="Send RFP" action="{!sendRFP1}" status="status" disabled="{!ISNULL(vendorList)}" id="rfpButton" />
            </apex:pageBlockButtons>

 

OR

 

You check if the list is null inside the sendRFP method and add a pagemessage to the visualforce Page.

 

public PageReference sendRFP() {
    	//Create a list to store the selected Vendors
    	List<Contact> selectedVendor = new List<Contact>();
    	//Loop through the Vendor search results and get the checked records
        if(vendorList==null) {

        ApexPages.add( new ApexPages.Message(ApexPages.ERROR, 'Please perform a search before sending RFP') );

        }

    	for(checkedVendor cV : vendorList) {
    		if(cV.checked == true) {
    			selectedVendor.add(cV.ven);
    		}
    	}
    	//Create a new RFP record for each selected Vendor
    	for(Contact sVen : selectedVendor){
    		Proposal__c p = new Proposal__c();
    		p.Vendor__c = sVen.Id;
    		p.Order__c = orderId;
    		insert p;
    	}
    	//Set Service Request Valuation Order Date
    	Order__c ord = [SELECT Id, Valuation_Order_Date__c FROM Order__c WHERE Id = :orderId];
    	if(ord.Valuation_Order_Date__c == NULL){
    		ord.Valuation_Order_Date__c = system.today();
    	}
    	update ord;
    	
    	//Return user to the Order page
    	PageReference orderURL = new PageReference('/' + orderId);
    	return orderURL;
    }

 

 

Actually I don't know where you set the values for vendorList, but I prefer initializing a list. It is more handy to have an empty list, than a null reference.

All Answers

tsalbtsalb

Similar to this, but this isn't working - i still get the attempt to de-reference null object error?

 

    public PageReference sendRFP1() {
    	List<Contact> selectedVendor = new List<Contact>();
    	if(vendorList.size() <1 ) {
		ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR, 'Please search for vendors first');
		ApexPages.addMessage(myMsg);		
    	}
		else {
	    	for(checkedVendor cV : vendorList) {
	    		if(cV.checked == true) {
	    			selectedVendor.add(cV.ven);
	    		}
	    	}
	    	for(Contact sVen : selectedVendor){
	    		Proposal__c p = new Proposal__c();
	    		p.Vendor__c = sVen.Id;
	    		p.Order__c = orderId;
	    		insert p;
	    	}
	    	Order__c ord = [SELECT Id, Valuation_Order_Date__c FROM Order__c WHERE Id = :orderId];
	    	if(ord.Valuation_Order_Date__c == NULL){
	    		ord.Valuation_Order_Date__c = system.today();
	    	}
	    	update ord;
		}
	PageReference orderURL = new PageReference('/' + orderId);
	return orderURL;
    }

 

Damien_Damien_

You might be looking for something like:

 

if (vendorList == null || vendorList.size() < 1)
  return null;

 It would be a little more useful though if you specified what line it was erroring on.

 

You might not need the vendorList.size() < 1 part though.  I just put it in there because if your suggestion.  The below may also work;

 

if (vendorList == null)
  return null;
tsalbtsalb

Edited original post to reflect line 316 - i'll try your suggestion

Damien_Damien_

I think you would want the first option I gave you since if nothing is happening I would assume you don't want to update a value on your order.

Nisse Knudsen.ax1026Nisse Knudsen.ax1026

If your vendorList is null, then you can't even call the .size(), as the size of nothing is: null.

So you could take two approaches: 

 

Either you disable the SendRFP button while vendorList is null 

 

<apex:pageBlockButtons >
                <apex:commandButton value="Search" action="{!vendorSearch}" rerender="results" status="status" id="searchButton" />
                <apex:commandButton value="Send RFP" action="{!sendRFP1}" status="status" disabled="{!ISNULL(vendorList)}" id="rfpButton" />
            </apex:pageBlockButtons>

 

OR

 

You check if the list is null inside the sendRFP method and add a pagemessage to the visualforce Page.

 

public PageReference sendRFP() {
    	//Create a list to store the selected Vendors
    	List<Contact> selectedVendor = new List<Contact>();
    	//Loop through the Vendor search results and get the checked records
        if(vendorList==null) {

        ApexPages.add( new ApexPages.Message(ApexPages.ERROR, 'Please perform a search before sending RFP') );

        }

    	for(checkedVendor cV : vendorList) {
    		if(cV.checked == true) {
    			selectedVendor.add(cV.ven);
    		}
    	}
    	//Create a new RFP record for each selected Vendor
    	for(Contact sVen : selectedVendor){
    		Proposal__c p = new Proposal__c();
    		p.Vendor__c = sVen.Id;
    		p.Order__c = orderId;
    		insert p;
    	}
    	//Set Service Request Valuation Order Date
    	Order__c ord = [SELECT Id, Valuation_Order_Date__c FROM Order__c WHERE Id = :orderId];
    	if(ord.Valuation_Order_Date__c == NULL){
    		ord.Valuation_Order_Date__c = system.today();
    	}
    	update ord;
    	
    	//Return user to the Order page
    	PageReference orderURL = new PageReference('/' + orderId);
    	return orderURL;
    }

 

 

Actually I don't know where you set the values for vendorList, but I prefer initializing a list. It is more handy to have an empty list, than a null reference.

This was selected as the best answer
tsalbtsalb

Interesting method to hide the RFP button - let me revisit this in a sec - I just got pulled into something. The list is initialized after the user hits "Search" . so yes, the list is null prior to hitting seach.

Nisse Knudsen.ax1026Nisse Knudsen.ax1026

By using 

disabled="true"

 the button only gets non-selectable.

 

If you would like to hide it, try either

rendered="false"

 or, if you would like to use css manually

style="visibility:{!IF(ISNULL(vendorList),'hidden','visible'};"

 

tsalbtsalb

Thank you Nisse - that did the trick. Had to re-render the button section after search - so, minor tweak. The final code changes:

 

<apex:pageBlock title="Vendor Search" id="SearchBlock" mode="edit">          
    <apex:pageBlockButtons >
        <apex:commandButton value="Search" action="{!vendorSearch}" rerender="results, SearchBlock" status="status" id="searchButton" />
        <apex:commandButton value="Send RFP" action="{!sendRFP}" status="status" id="rfpButton" disabled="{!ISNULL(vendorList)}"/>
    </apex:pageBlockButtons>