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
gaurav.sfdcgaurav.sfdc 

Adding opportunities from VF

I created a Opp List in controller and bind it with repeater, In VF page I used InputFild but on saving it gives Null pointer exception but when I uses InputText it works, problem is I cannot use inputText for CloseDate and Stages, what could be the issue with InputFiled
Best Answer chosen by gaurav.sfdc
Adnubis LLCAdnubis LLC
Found the problem. 

The null exception was because you were trying to access c.LastName.length when LastName was null. The same thing with the opp.Name.Length. When you use a inputText the value isn't null so the .length doesn't cause an error. Here  is your controller code with two additional checks for null values. I have tested in my developer account and it works. 

public class AddDataController {
   
    public List<Contact>  ContactList {get;set;}
    public List<Opportunity>  OppList {get;set;}
    public List<Contact> finalContactList {get;set;}
    public List<Opportunity> finalOppList {get;set;}
    private final Account acct;

public AddDataController(ApexPages.StandardController stdController ) {
        this.acct = (Account)stdController.getRecord();
        ContactList = new List<Contact>();
        OppList = new List<Opportunity>();
        for (integer i=0;i<3;i++){
            Contact C = new Contact();
            ContactList.add(C);
            Opportunity O = new Opportunity();
            OppList.add(O);
        }
    }

    public void AddCon() {
        Contact C=new Contact();
        ContactList.add(C);
    }
   
    public void AddOpp() {
        Opportunity O=new Opportunity ();
        OppList.add(O);
    }

   Public PageReference Save() {
    
     finalContactList = new List<Contact>();
     finalOppList = new List<Opportunity>();
    
     for (Contact c : ContactList) {
       c.Account = this.acct;
        if(c.LastName != null && c.LastName.length()<>0){
            finalContactList.add(c);
         }
     }
    
     for (Opportunity o : OppList){
       if(o.Name != null && o.Name.length()<>0){
           finalOppList.add(o);
       }
     }
    
     if(finalContactList.size() >  0)
        insert finalContactList;
     
     if(finalOppList.size() >  0)
        insert finalOppList;

      return Apexpages.currentPage();
   }

}

All Answers

Adnubis LLCAdnubis LLC
Can you post the code you are working with to give us a better idea what is going on. Typically when I get that error when dealing with lists its because i try to manipulate them before I instantiate the actual list. I make this mistake most often when I use the shorthand getter setter to make it easily available to the page. See below. 

public list<Opportunity> opps {get;set;}

public void addOpp(Opportunity opp){
   opps.add(opp);


Make sure that your opps list has been instantiated before you attempt to add anything to it. This is just an example to get the point across, there are better ways to handle this situation if it ends up being your problem. If not please post some code and I will troubleshoot it.

public void addOpp(Opportunity opp){
   if(this.opps == null)  {
       this.opps = new list<Opportunity>();
   }
   opps.add(opp);
}
gaurav.sfdcgaurav.sfdc
Thanks for your reply, If that was the case then why it is not giving error with inputText, Any ways here is my code
VF Page
------------
<apex:page standardController="Account" extensions="AddDataController">
<apex:form >
   <apex:sectionHeader title="Enter Contacts and Opportunities for Account"/>
 
    <apex:PageBlock title="Account: {!Account.Name}" mode="edit">
    <apex:PageBlockButtons >
     <apex:commandButton action="{!save}" value="  Save  "/>
    </apex:PageBlockButtons>
       <apex:pageblockSection title="Contacts" columns="1" >
           <apex:CommandButton Value="Add more Contact" action="{!AddCon}" />
           <apex:repeat value="{!ContactList}" var="contact" >
           <div class="row">
               <apex:inputField required="False" styleClass="Field" value="{!Contact.LastName}"/>
           </div>
           </apex:repeat>
       </apex:pageblockSection>
       <apex:pageblockSection title="Opportunities" columns="1" >
           <apex:CommandButton Value="Add more Opp"  action="{!AddOpp}"/>
           <apex:repeat value="{!OppList}" var="opp" >
           <div class="row">
               <apex:inputField required="False" styleClass="Field" value="{!opp.Name}"/>
               <apex:inputField required="False" styleClass="Field" value="{!opp.CloseDate}"/>
               <apex:inputField required="False" styleClass="Field" value="{!opp.StageName}"/>
           </div>
           </apex:repeat>
       </apex:pageblockSection>
    </apex:PageBlock>
</apex:form>
</apex:page>

Controller
---------------------

public class AddDataController {
    public List<Contact>  ContactList {get;set;}
    public List<Opportunity>  OppList {get;set;}
    public List<Contact> finalContactList {get;set;}
    public List<Opportunity> finalOppList {get;set;}
    private final Account acct;

     public void AddCon() {
      
        Contact C=new Contact();
        ContactList.add(C);
    }
    public void AddOpp() {
        Opportunity O=new Opportunity ();
        OppList.add(O);
    }
    public AddDataController(ApexPages.StandardController stdController ) {
        this.acct = (Account)stdController.getRecord();
        ContactList = new List<Contact>();
        OppList = new List<Opportunity>();
        for (integer i=0;i<3;i++)
        {
            Contact C=new Contact();
            ContactList.add(C);
            Opportunity O = new Opportunity();
            OppList.add(O);
        }
    }
  
   Public PageReference Save()
   {
     //insert contacts
     finalContactList = new List<Contact>();
     finalOppList = new List<Opportunity>();
     for (Contact c : ContactList)
     {
       c.Account = this.acct;
      if(c.LastName.length()<>0)
       {
           finalContactList.add(c);
       }
     }
     for (Opportunity o : OppList)
     {
       if(o.Name.length()<>0)
       {
           finalOppList.add(o);
       }
     }
     if(finalContactList.size() >  0)
        insert finalContactList;
      
     if(finalOppList.size() >  0)
        insert finalOppList;
      
     return Apexpages.currentPage();
   }

}


if you replace inputfield with inputtext in contact section then this error does not come but then come for opportunity list, if i relace all fields with text then date field and stage fields looks odd.
Adnubis LLCAdnubis LLC
Found the problem. 

The null exception was because you were trying to access c.LastName.length when LastName was null. The same thing with the opp.Name.Length. When you use a inputText the value isn't null so the .length doesn't cause an error. Here  is your controller code with two additional checks for null values. I have tested in my developer account and it works. 

public class AddDataController {
   
    public List<Contact>  ContactList {get;set;}
    public List<Opportunity>  OppList {get;set;}
    public List<Contact> finalContactList {get;set;}
    public List<Opportunity> finalOppList {get;set;}
    private final Account acct;

public AddDataController(ApexPages.StandardController stdController ) {
        this.acct = (Account)stdController.getRecord();
        ContactList = new List<Contact>();
        OppList = new List<Opportunity>();
        for (integer i=0;i<3;i++){
            Contact C = new Contact();
            ContactList.add(C);
            Opportunity O = new Opportunity();
            OppList.add(O);
        }
    }

    public void AddCon() {
        Contact C=new Contact();
        ContactList.add(C);
    }
   
    public void AddOpp() {
        Opportunity O=new Opportunity ();
        OppList.add(O);
    }

   Public PageReference Save() {
    
     finalContactList = new List<Contact>();
     finalOppList = new List<Opportunity>();
    
     for (Contact c : ContactList) {
       c.Account = this.acct;
        if(c.LastName != null && c.LastName.length()<>0){
            finalContactList.add(c);
         }
     }
    
     for (Opportunity o : OppList){
       if(o.Name != null && o.Name.length()<>0){
           finalOppList.add(o);
       }
     }
    
     if(finalContactList.size() >  0)
        insert finalContactList;
     
     if(finalOppList.size() >  0)
        insert finalOppList;

      return Apexpages.currentPage();
   }

}
This was selected as the best answer
gaurav.sfdcgaurav.sfdc
Hey thanks, it worked but still confused, Why Last name is null when I am giving some value? Inputtype also displays it textFiled. There is a value, it gets inserted also but if you remove Null check it throws error, bit strange
Adnubis LLCAdnubis LLC
I am glad that worked for you. I am not 100% sure why it works with inputText but not inputField but my best guess is that it has to do with how salesforce interprets the two elements. InputText must be evaluated as an empty string which would return a length of 0. Where as the inputField is bound directly to the objects field and is interpreted as Null which does not have the .length method. Again I am not 100% sure on this but that is how I am interpreting the situation for now. Thanks for marking solved.