You need to sign in to do that
Don't have an account?

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>
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
OR
You check if the list is null inside the sendRFP method and add a pagemessage to the visualforce Page.
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
Similar to this, but this isn't working - i still get the attempt to de-reference null object error?
You might be looking for something like:
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;
Edited original post to reflect line 316 - i'll try your suggestion
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.
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
OR
You check if the list is null inside the sendRFP method and add a pagemessage to the visualforce Page.
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.
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.
By using
the button only gets non-selectable.
If you would like to hide it, try either
or, if you would like to use css manually
Thank you Nisse - that did the trick. Had to re-render the button section after search - so, minor tweak. The final code changes: