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
❤Code❤Code 

getting null pointer exxception

Hi All,

I am getting NPE error in line no 55. Can anybody help me out in this how to resolve this . 

Apex Class - 

    public with sharing class soption1
    {

        public soption1() {

        }

        SLA__c account;
        SOC__c contact;
        MSA__c opportunity;
            
        public List<MSA__C> records{get;set;}
        public MSA__c objAct{get;set;}
        public List<MSAWrapper> MSAWrapperList {get; set;}
       
        public soption1(ApexPages.StandardController controller) 
        {
            objAct = new MSA__C();
            MSAWrapperList = new List<MSAWrapper>();
        }
        
        public SLA__c getAccount() {
        if(account == null) account = new SLA__c();
        return account;
        }
        
        public SOC__c getContact() {
        if(contact == null) contact = new SOC__c();
        return contact;
       }
        
        public PageReference step1() {
        return Page.newOpptyStep11;
        }
       
        public PageReference step2() {
          return Page.testmulti1;
        }
        
        
        public pagereference search()
        {
            MSAWrapperList.clear();
            records=[Select ID,Name,Coe__c,Dept__c,OpCo__c from MSA__C  where Coe__c=: objAct.Coe__c and Dept__c=: objAct.Dept__c];
            for(MSA__C obj : records)
            {
                MSAWrapperList.add( new MSAWrapper(obj) );
            }
           return null;
        }

        public PageReference processSelected() 
        {
        
          account.Name = contact.Name;  /// errror here
          insert account;
          
            List<MSA__C> selectedMSA = new List<MSA__C>();
            for(MSAWrapper cCon: MSAWrapperList ) 
            {
                if(cCon.selected == true) 
                {
                    selectedMSA.add(cCon.msaObj);
                }
            }
            for(MSA__C msa: selectedMSA) 
            {
                    system.debug(msa);
                    SOC__c so = new SOC__c();
                    so.Name = msa.Name;
                    so.SLA__c = account.id;
                    insert so;
                // Please add your login here
            }
            return null;
            
            
        }
        
        
        public class MSAWrapper 
        {
            public MSA__C msaObj {get; set;}
            public Boolean selected {get; set;}
            public MSAWrapper(MSA__C c) {
                msaObj = c;
                selected = false;
            }
        }
         
    }

Regards
Best Answer chosen by ❤Code
Eswar Prasad@Sfdc11Eswar Prasad@Sfdc11
when error is null pointer exception means that memory not created in particular sobject now i assign try it below code.
public with sharing class soption1
    {

        public soption1() {
    account=new SLA__c();
    contact=new SOC__c();
   opportunity=new MSA__c();
  records=new List<MSA__C>();
objAct=new MSA__c();
msawrapperlist=new List<MSAWrapper>();
        }

        public SLA__c account{set;get;};
        public SOC__c contact{set;get;};
       public MSA__c opportunity{set;get;};
            
        public List<MSA__C> records{get;set;}
        public MSA__c objAct{get;set;}
        public List<MSAWrapper> MSAWrapperList {get; set;}
       
        public soption1(ApexPages.StandardController controller) 
        {
            objAct = new MSA__C();
            MSAWrapperList = new List<MSAWrapper>();
        }
        
        public SLA__c getAccount() {
        if(account == null) account = new SLA__c();
        return account;
        }
        
        public SOC__c getContact() {
        if(contact == null) contact = new SOC__c();
        return contact;
       }
        
        public PageReference step1() {
        return Page.newOpptyStep11;
        }
       
        public PageReference step2() {
          return Page.testmulti1;
        }
        
        
        public pagereference search()
        {
            MSAWrapperList.clear();
            records=[Select ID,Name,Coe__c,Dept__c,OpCo__c from MSA__C  where Coe__c=: objAct.Coe__c and Dept__c=: objAct.Dept__c];
            for(MSA__C obj : records)
            {
                MSAWrapperList.add( new MSAWrapper(obj) );
            }
           return null;
        }

        public PageReference processSelected() 
        {
        
          account.Name = contact.Name;
          insert account;
          
            List<MSA__C> selectedMSA = new List<MSA__C>();
            for(MSAWrapper cCon: MSAWrapperList ) 
            {
                if(cCon.selected == true) 
                {
                    selectedMSA.add(cCon.msaObj);
                }
            }
            for(MSA__C msa: selectedMSA) 
            {
                    system.debug(msa);
                    SOC__c so = new SOC__c();
                    so.Name = msa.Name;
                    so.SLA__c = account.id;
                    insert so;
                // Please add your login here
            }
            return null;
            
            
        }
        
        
        public class MSAWrapper 
        {
            public MSA__C msaObj {get; set;}
            public Boolean selected {get; set;}
            public MSAWrapper(MSA__C c) {
                msaObj = c;
                selected = false;
            }
        }
         
    }

Hope this helps! Mark it as solution if this solves your problem.
Regards
Eswar Prasad.

All Answers

pconpcon
This is because you contact record is null.  I don't see where you are setting it, but you can change your code to "lazy load" it and move it up to fix it.
 
public with sharing class soption1
    public soption1() {}

    SLA__c account {
        get {
            if (account == null) {
                account = new SLA__c();
            }

            return account;
        }
        set;
    };

    SOC__c contact {
        get {
            if (contact == null) {
                contact = new SOC__c();
            }

            return contact;
        }
        set;
    };

    MSA__c opportunity;

    public List<MSA__C> records {get; set;}
    public MSA__c objAct {get; set;}
    public List<MSAWrapper> MSAWrapperList {get; set;}
    public soption1(ApexPages.StandardController controller) {
        objAct = new MSA__C();
        MSAWrapperList = new List<MSAWrapper>();
    }

    public PageReference step1() {
        return Page.newOpptyStep11;
    }

    public PageReference step2() {
        return Page.testmulti1;
    }

    public pagereference search() {
        MSAWrapperList.clear();

        records = [
            select Name,
                Coe__c,
                Dept__c,
                OpCo__c
            from MSA__C
            where Coe__c = :objAct.Coe__c and
                Dept__c = :objAct.Dept__c
        ];

        for (MSA__C obj : records) {
            MSAWrapperList.add(new MSAWrapper(obj));
        }

        return null;
    }

    public PageReference processSelected() {
        account.Name = contact.Name;
        insert account;

        List<MSA__C> selectedMSA = new List<MSA__C>();
        for (MSAWrapper cCon : MSAWrapperList) {
            if (cCon.selected) {
                selectedMSA.add(cCon.msaObj);
            }
        }

        List<SOC__c> socList = new List<SOC__c>();

        for (MSA__C msa : selectedMSA) {
                socList.add(new SOC__c(
                    Name = msa.Name,
                    SLA__c = account.id
                ));
        }

        if (!socList.isEmpty()) {
            insert socList;
        }

        return null;
    }

    public class MSAWrapper {
        public MSA__C msaObj {get; set;}
        public Boolean selected {get; set;}
        public MSAWrapper(MSA__C c) {
            msaObj = c;
            selected = false;
        }
    }
}

NOTE: This code has not been tested and may contain typographical or logical errors.
❤Code❤Code
Hi pcon,

I am trying to create a wizard kind of functionality. I am still getting the NPE error . Below is my vf pages and apex classess. Kindly check and help me to know where to modify.

Apex Class - 

    public with sharing class soption1
    {

        public soption1() {

        }

        SLA__c account;
        SOC__c contact;
        MSA__c opportunity;
            
        public List<MSA__C> records{get;set;}
        public MSA__c objAct{get;set;}
        public List<MSAWrapper> MSAWrapperList {get; set;}
       
        public soption1(ApexPages.StandardController controller) 
        {
            objAct = new MSA__C();
            MSAWrapperList = new List<MSAWrapper>();
        }
        
        public SLA__c getAccount() {
        if(account == null) account = new SLA__c();
        return account;
        }
        
        public SOC__c getContact() {
        if(contact == null) contact = new SOC__c();
        return contact;
       }
        
        public PageReference step1() {
        return Page.newOpptyStep11;
        }
       
        public PageReference step2() {
          return Page.testmulti1;
        }
        
        
        public pagereference search()
        {
            MSAWrapperList.clear();
            records=[Select ID,Name,Coe__c,Dept__c,OpCo__c from MSA__C  where Coe__c=: objAct.Coe__c and Dept__c=: objAct.Dept__c];
            for(MSA__C obj : records)
            {
                MSAWrapperList.add( new MSAWrapper(obj) );
            }
           return null;
        }

        public PageReference processSelected() 
        {
        
          account.Name = contact.Name;
          insert account;
          
            List<MSA__C> selectedMSA = new List<MSA__C>();
            for(MSAWrapper cCon: MSAWrapperList ) 
            {
                if(cCon.selected == true) 
                {
                    selectedMSA.add(cCon.msaObj);
                }
            }
            for(MSA__C msa: selectedMSA) 
            {
                    system.debug(msa);
                    SOC__c so = new SOC__c();
                    so.Name = msa.Name;
                    so.SLA__c = account.id;
                    insert so;
                // Please add your login here
            }
            return null;
            
            
        }
        
        
        public class MSAWrapper 
        {
            public MSA__C msaObj {get; set;}
            public Boolean selected {get; set;}
            public MSAWrapper(MSA__C c) {
                msaObj = c;
                selected = false;
            }
        }
         
    }

Vf Page 1 - 

    <apex:page controller="soption1"
               tabStyle="Opportunity">
      <apex:sectionHeader title="New Customer Opportunity"
                          subtitle="Step 1 of 3"/>
      <apex:form >
        <apex:pageBlock title="Customer Information">

          <!-- This facet tag defines the "Next" button that appears
               in the footer of the pageBlock. It calls the step2()
               controller method, which returns a pageReference to
               the next step of the wizard. -->  
        
          <apex:facet name="footer">
            <apex:commandButton action="{!step2}" value="Next"
                                styleClass="btn"/>
          </apex:facet>
          <apex:pageBlockSection title="Account Information">

          <!-- <apex:panelGrid> tags organize data in the same way as
                a table. It places all child elements in successive cells,
                in left-to-right, top-to-bottom order -->  
        
          <!-- <apex:outputLabel > and <apex:inputField > tags can be 
                bound together with the for and id attribute values,
                respectively. -->  
        
           <apex:panelGrid columns="2">
            <apex:outputLabel value="Account Name" for="accountName"/>
            <apex:inputField id="accountName" value="{!account.name}"/>

           </apex:panelGrid>
          </apex:pageBlockSection>
          
        </apex:pageBlock>
      </apex:form>
    </apex:page>

Vf Page 2 - 

<apex:page standardController="MSA__c" extensions="soption1" sidebar="false"> 
<apex:messages style="color:red"></apex:messages>
    <apex:form id="theform">
    
            <apex:pageblock mode="edit">
                <apex:pageblockSection >
                   <apex:inputfield value="{!objAct.Coe__c}" />
                   <apex:inputfield value="{!objAct.Dept__c}" />
                   <apex:inputfield value="{!objAct.OpCo__c }" />
                </apex:pageblockSection>

                <apex:pageBlockButtons >
                    <apex:commandButton value="Search" action="{!search}" />
                </apex:pageBlockButtons>
            </apex:pageblock>

            <apex:pageBlock >
                <apex:pageBlockButtons >
                    <apex:commandButton value="Process Selected" action="{!processSelected}" rerender="table"/>
                </apex:pageBlockButtons>
                <apex:pageBlockTable value="{!MSAWrapperList}" var="obj" id="table">
                     <apex:column >
                        <apex:inputCheckbox value="{!obj.selected}"/>
                    </apex:column>
                    <apex:column value="{!obj.msaObj.Name}" />
                </apex:pageBlockTable>
            </apex:pageBlock>
            
    </apex:form>
</apex:page>


Regards

 
Sumitkumar_ShingaviSumitkumar_Shingavi
Try below code:
 
public with sharing class soption1 {

	public soption1() {

	}
	
	//Declare and define
	SLA__c account = new SLA__c();
	SOC__c contact = new SOC__c();
	MSA__c opportunity = new MSA__c();

	public List<MSA__C> records{get;set;}
	public MSA__c objAct{get;set;}
	public List<MSAWrapper> MSAWrapperList {get; set;}
   
	public soption1(ApexPages.StandardController controller) 
	{
		objAct = new MSA__C();
		MSAWrapperList = new List<MSAWrapper>();
	}
	
	public SLA__c getAccount() {
	if(account == null) account = new SLA__c();
	return account;
	}
	
	public SOC__c getContact() {
	if(contact == null) contact = new SOC__c();
	return contact;
   }
	
	public PageReference step1() {
	return Page.newOpptyStep11;
	}
   
	public PageReference step2() {
	  return Page.testmulti1;
	}
	
	
	public pagereference search()
	{
		MSAWrapperList.clear();
		records=[Select ID,Name,Coe__c,Dept__c,OpCo__c from MSA__C  where Coe__c=: objAct.Coe__c and Dept__c=: objAct.Dept__c];
		for(MSA__C obj : records)
		{
			MSAWrapperList.add( new MSAWrapper(obj) );
		}
	   return null;
	}

	public PageReference processSelected() 
	{
	
	  account.Name = contact.Name;
	  insert account;
	  
		List<MSA__C> selectedMSA = new List<MSA__C>();
		for(MSAWrapper cCon: MSAWrapperList ) 
		{
			if(cCon.selected == true) 
			{
				selectedMSA.add(cCon.msaObj);
			}
		}
		for(MSA__C msa: selectedMSA) 
		{
				system.debug(msa);
				SOC__c so = new SOC__c();
				so.Name = msa.Name;
				so.SLA__c = account.id;
				insert so;
			// Please add your login here
		}
		return null;		
	}
		
	public class MSAWrapper 
	{
		public MSA__C msaObj {get; set;}
		public Boolean selected {get; set;}
		public MSAWrapper(MSA__C c) {
			msaObj = c;
			selected = false;
		}
	}	 
}

Hope this helps! Mark it as solution if this solves your problem.
 
Eswar Prasad@Sfdc11Eswar Prasad@Sfdc11
when error is null pointer exception means that memory not created in particular sobject now i assign try it below code.
public with sharing class soption1
    {

        public soption1() {
    account=new SLA__c();
    contact=new SOC__c();
   opportunity=new MSA__c();
  records=new List<MSA__C>();
objAct=new MSA__c();
msawrapperlist=new List<MSAWrapper>();
        }

        public SLA__c account{set;get;};
        public SOC__c contact{set;get;};
       public MSA__c opportunity{set;get;};
            
        public List<MSA__C> records{get;set;}
        public MSA__c objAct{get;set;}
        public List<MSAWrapper> MSAWrapperList {get; set;}
       
        public soption1(ApexPages.StandardController controller) 
        {
            objAct = new MSA__C();
            MSAWrapperList = new List<MSAWrapper>();
        }
        
        public SLA__c getAccount() {
        if(account == null) account = new SLA__c();
        return account;
        }
        
        public SOC__c getContact() {
        if(contact == null) contact = new SOC__c();
        return contact;
       }
        
        public PageReference step1() {
        return Page.newOpptyStep11;
        }
       
        public PageReference step2() {
          return Page.testmulti1;
        }
        
        
        public pagereference search()
        {
            MSAWrapperList.clear();
            records=[Select ID,Name,Coe__c,Dept__c,OpCo__c from MSA__C  where Coe__c=: objAct.Coe__c and Dept__c=: objAct.Dept__c];
            for(MSA__C obj : records)
            {
                MSAWrapperList.add( new MSAWrapper(obj) );
            }
           return null;
        }

        public PageReference processSelected() 
        {
        
          account.Name = contact.Name;
          insert account;
          
            List<MSA__C> selectedMSA = new List<MSA__C>();
            for(MSAWrapper cCon: MSAWrapperList ) 
            {
                if(cCon.selected == true) 
                {
                    selectedMSA.add(cCon.msaObj);
                }
            }
            for(MSA__C msa: selectedMSA) 
            {
                    system.debug(msa);
                    SOC__c so = new SOC__c();
                    so.Name = msa.Name;
                    so.SLA__c = account.id;
                    insert so;
                // Please add your login here
            }
            return null;
            
            
        }
        
        
        public class MSAWrapper 
        {
            public MSA__C msaObj {get; set;}
            public Boolean selected {get; set;}
            public MSAWrapper(MSA__C c) {
                msaObj = c;
                selected = false;
            }
        }
         
    }

Hope this helps! Mark it as solution if this solves your problem.
Regards
Eswar Prasad.
This was selected as the best answer