Easy Save 2 Argument Cannot Be Null
Welcome to Support!
Search for an answer or ask a question of the zone or Customer Support.
Need help? Dismiss
- This Question
Borsalino
System.NullPointerException: Argument 2 cannot be null
Hi guys,
I have been developing this code which reads a csv file and then upload it to our custom object called In_Kind.
We were encountering these error argument 2 cannot be null but we cannot trace which part of the code makes that argument 2 null. We even can't find these argument 2 because the error doesn't point which line code makes the error.
Here is the sample csv file:
Sample CSV
This is my code:
Data_Loader Page 1
<apex:page sidebar="true" controller="Data_Loader_Controller" > <apex:sectionHeader title="Good 360" subtitle="Data Loader"/> <apex:pageBlock title="Step 1: Upload Screen" mode="edit"> <apex:pagemessages /> <apex:pageBlockSection title="Upload CSV File" columns="1"> <center> <apex:form > <apex:inputFile value="{!contentFile}" filename="{!nameFile}" /> <apex:commandButton action="{!ReadFile}" value="Upload File" id="theButton" style="width:70px;"/> <br/><font color="red"><b>Note: Please use the standard template to upload Organizations. <a href="{!URLFOR($Resource.InKind_DataLoader)}" target="_blank"> Click here </a> to download the template. </b> </font> <br/> <!-- <apex:outputLabel value="File Name" for="fileName"/> <apex:inputText value="{!fileName}" id="fileName"/> --> </apex:form> </center> </apex:pageBlockSection> </apex:pageBlock> </apex:page>
Data Loader Page 2
<apex:page sidebar="false" controller="Data_Loader_Controller" > <apex:sectionHeader title="Good 360" subtitle="Data Loader"/> <apex:pageBlock title="Step 2: Confirmation Screen" mode="edit"> <apex:pagemessages /> <apex:facet name="footer"> <apex:form > <apex:commandButton action="{!step1}" value="Previous" styleClass="btn"/> <apex:commandButton action="{!save}" value="Save" styleClass="btn"/> </apex:form> </apex:facet> <apex:pageBlockSection title="Chorva" columns="1"> <p><b>No. of Row/s:</b> {!rowCount}</p> <apex:pageblocktable title="CSV File Details" value="{!uploadedAccounts}" var="data" rendered="{!NOT(ISNULL(uploadedAccounts))}"> <apex:column headerValue="Order Number"> <apex:outputField value="{!data.Good360_Order_Number__c}"/> </apex:column> <apex:column headerValue="Date"> <apex:outputField value="{!data.Date_of_Donation__c}"/> </apex:column> <apex:column headerValue="Quantity"> <apex:outputField value="{!data.Quantity__c}"/> </apex:column> <apex:column headerValue="FMV"> <apex:outputField value="{!data.Market_Value__c}"/> </apex:column> <apex:column headerValue="Recipient Charity"> <apex:outputField value="{!data.NGO__c}"/> </apex:column> <apex:column headerValue="Country"> <apex:outputField value="{!data.Country__c}"/> </apex:column> <apex:column headerValue="Contact"> <apex:outputField value="{!data.Contact__c}"/> </apex:column> </apex:pageblocktable> </apex:pageBlockSection> </apex:pageBlock> </apex:page>
Data Loader Controller:
public class Data_Loader_Controller { public String nameFile{get;set;} public Blob contentFile{get;set;} String[] filelines = new String[]{}; public Integer rowCount { get; set; } boolean isDisabled = true; public String fileName { get; set; } List<In_Kind__c> datatoupload = new List<In_Kind__c>(); List<Contact> contacts; List<Account> accounts; /* This variable sets the max number of rows */ public Integer maxRows = 1000; /* This function is for disabling the "Next" button at the beginning */ public Boolean getIsDisabled() { return this.isDisabled; } /* This function is for loading Step 1 of the Data Loader */ public PageReference step1() { return Page.Data_Loader; } public PageReference step2 () { return Page.Data_Loader_2; } /* This function is for saving the uploaded csv file to the In-Kind Custom Object */ public PageReference save() { try { insert datatoupload; return Page.Data_Loader_Success; } catch (System.DmlException e) { ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.ERROR,'Please correct the errors'); ApexPages.addMessage(errormsg); } return null; } // This function converts "Onsite Due Date" to Date data type private Date toDate(String sdate) { String[] splitdate = sdate.split('/'); Date myDate = date.newinstance(Integer.valueOf(splitdate[2]), Integer.valueOf(splitdate[0]), Integer.valueOf(splitdate[1])); return myDate; } /* // This function checks if the organization exists private String findOrg(String name, Account o){ Account a = new Account(); //org = new List<Account>(); try{ a = [ SELECT Id FROM Account WHERE name = :name LIMIT 1]; } catch(System.QueryException e){ insert o; update o; //org.add(o); a = [ SELECT Id FROM Account WHERE name = :name LIMIT 1]; } return a.Id; } // This function checks if the contact exists private String findContacxt(String email, Contact o){ Contact c; try { c = [ SELECT Id FROM Contact WHERE email = :email LIMIT 1]; } catch(System.QueryException e) { insert o; update o; c = [ SELECT Id FROM Contact WHERE email = :email LIMIT 1]; } return c.Id; } */ /* This function removes the extra space at the end of each column */ private String removeSpace(String data) { if(data.substring(data.length()-1) == ' ') return data.substring(0,data.length()-1); else return data; } //Select all from emails list, then add missing contacts private void getContactList() { try { contacts = [ SELECT Id, Email FROM Contact]; } catch(System.QueryException e) { } } private void getOrgList(){ try { accounts = [ SELECT Id, Name FROM Account]; } catch(System.QueryException e) { } } private String findContact(String conemail){ Integer i = 0; for(i=0;i<contacts.size();i++){ if(contacts[i].Email.equals(conemail)){ return contacts[i].Id; } } return 'NotFound'; } private String findOrg(String accname){ Integer i = 0; for(i=0;i<accounts.size();i++){ if(accounts[i].Name.equals(accname)){ return accounts[i].Id; } } return 'NotFound'; } /* This function parses the csv and stores it in the array list */ public Pagereference ReadFile() { try { nameFile=contentFile.toString(); filelines = nameFile.split('\n'); //select the 2 lists from database getOrgList(); getContactList(); // This statement is for limiting the max number of rows if (filelines.size() > maxRows) { ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.ERROR,'You have exceeded the max number of rows. Limit:' +maxRows); ApexPages.addMessage(errormsg); return null; } /* How the CSV file looks like: 0 Date 1 Order Number 2 Quantity 3 Type of item 4 Item Number 5 Charitable (Market value perhaps?) 6 Total Contribution (or this is the market value?) 7 EIN or NCES 8 Charity Name (NGO) 9 Address/Street 10 City 11 State 12 Zip 13 Contact Name 14 Phone 15 Email */ //check for existence of orgs, add if non-existent for (Integer i=1;i<filelines.size();i++) { String[] inputvalues = new String[]{}; /* Splits columns and rows */ inputvalues = filelines[i].split(','); Account a = new Account(); // Fields for Account a.Name = removeSpace(inputvalues[8]); a.BillingStreet = removeSpace(inputvalues[9]); a.BillingCity = removeSpace(inputvalues[10]); a.BillingState = removeSpace(inputvalues[11]); a.BillingPostalCode = removeSpace(inputvalues[12]); if(findOrg(a.Name)=='NotFound') insert a; } //check for existence of contacts, add if non-existent for (Integer i=1;i<filelines.size();i++) { String[] inputvalues = new String[]{}; /* Splits columns and rows */ inputvalues = filelines[i].split(','); Contact c = new Contact(); Integer space = inputvalues[13].indexOf(' '); //Fields for Contact c.FirstName = inputvalues[13].substring(0, space); c.LastName = removeSpace(inputvalues[13].substring(space+1)); c.Email = removeSpace(inputvalues[15]); c.AccountId = findOrg(removeSpace(inputvalues[8])); c.Phone = removeSpace(inputvalues[14]); if(findContact(c.Email)=='NotFound') insert c; } //refresh the 2 lists after adding new records getOrgList(); getContactList(); for (Integer i=1;i<filelines.size();i++) { String[] inputvalues = new String[]{}; /* Splits columns and rows */ inputvalues = filelines[i].split(','); Contact c = new Contact(); Account a = new Account(); In_Kind__c k = new In_Kind__c(); Integer space = inputvalues[13].indexOf(' '); // Fields for In-Kind k.Good360_Order_Number__c = removeSpace(inputvalues[1]); k.Date_of_Donation__c = toDate(removeSpace(inputvalues[0])); k.Quantity__c = Integer.valueOf(removeSpace(inputvalues[2])); // There should be a new line for type of item k.Market_Value__c = Decimal.valueOf(removeSpace(inputvalues[5]).substring(1)); k.NGO__c = findOrg(removeSpace(inputvalues[8])); k.Country__c = 'USA'; //removeSpace(inputvalues[10]); k.Contact__c = findContact(inputvalues[15].substring(0, inputvalues[15].length()-1)); k.Project__c = '006L0000002wOIn'; // This is the id of the Project: "Tech Warehouse Donation Program" datatoupload.add(k); } rowCount = datatoupload.size(); } catch (System.NullPointerException e) { ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.ERROR,'Please select a file to upload'); ApexPages.addMessage(errormsg); return null; } catch (System.StringException e) { ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.ERROR,'Please Upload a CSV File or check the proper template or try again later'); ApexPages.addMessage(errormsg); return null; } catch (System.ListException e) { ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.ERROR,'Please check the proper template'); ApexPages.addMessage(errormsg); return null; } return Page.Data_Loader_2; } /* This function returns the array list with the data */ public List<In_Kind__c> getuploadedAccounts() { if (datatoupload!= NULL){ this.isDisabled = false; return datatoupload; } else return null; } }
You need to sign in to do that.
Dismiss
parduehatabligh67.blogspot.com
Source: https://developer.salesforce.com/forums/?id=906F000000093ucIAA
Postar um comentário for "Easy Save 2 Argument Cannot Be Null"