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
Keenan WojniczKeenan Wojnicz 

Keep getting a null object error, I don't understand!

Hi,

I'm trying to deploy a new visualforce page but I keep getting this error, I don't understand why it's happening.
I am in dire need of some assistance!

The error says: 
Apex script unhandled exception by user/organization: 00537000000iFK0/00D210000000MSY
Source organization: 00D37000000Pn3O (null)
Visualforce Page: /apex/BusinessIssue
caused by: System.NullPointerException: Attempt to de-reference a null object
Class.BusinessIssueController.<init>: line 20, column 1
 
<apex:page standardController="CriticalBusinessIssue__c" extensions="BusinessIssueController" tabStyle="CriticalBusinessIssue__c">
<style>
    .startStyle{
        font-size:12px;
        font-weight:bold;
        color:red;
    }
</style>

    <apex:sectionHeader title="Business Issue" subtitle="Critical Business Issue" />
    <apex:form id="frm">
        <apex:actionFunction name="addIssue" action="{!AddIssue}" status="addBI" reRender="frm" />
        <apex:pageMessages escape="false" />
        <apex:pageBlock >
            <apex:pageBlockButtons >
                <apex:commandButton value="Save Business Issues" action="{!Save}"/>
            </apex:pageBlockButtons>
            <apex:pageBlockSection title="New Critical Business Issue" columns="1">
                <apex:actionStatus id="addBI" startText="Adding..." startStyleClass="startStyle"/>
                <apex:pageBlockTable value="{!BusinessIssueModels}" var="bim">
                    <apex:column headerValue="Sr. No." value="{!bim.srNo}" />
                    <apex:column headerValue="Critical Business Issue">
                        <apex:inputField value="{!bim.businessIssue.Body__c}" style="width:500px;"/>
                    </apex:column>
                 </apex:pageBlockTable>
            </apex:pageBlockSection>
            <apex:commandButton value="Add More Business Issues" onclick="addIssue(); return false;" />
       </apex:pageBlock>
       <center></center>  
    </apex:form>
</apex:page>

 
Best Answer chosen by Keenan Wojnicz
Abhishek BansalAbhishek Bansal
Hi Keenan,

I dont understand why the error is coming.
It would be good if you can contact me personally on my gmail Id or Skype so that i can look at your issue in a better way and will surely provide you help.
Gmail : abhibansal2790@gmail.com
SkypeId : abhishek.bansal2790

Let me know if you have any other way in which i can provide you help.

Thanks,
Abhishek

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Can you please post your "BusinessIssueController" class
Keenan WojniczKeenan Wojnicz
public class BusinessIssueController{
    public CriticalBusinessIssue__c businessIssue{get;set;}
    public LIST<CriticalBusinessIssue__c> businessIssueList{get;set;}
    public LIST<BusinessIssueModel> BusinessIssueModels{get;set;}
    public String id;
    public String returnUrl;
    //Constructor
    public BusinessIssueController(ApexPages.StandardController sc){
        businessIssue = new CriticalBusinessIssue__c();
        BusinessIssueModels = new LIST<BusinessIssueModel>();
        String paramKey = Opportunity_App__c.getAll().get('Opp_Param_Key').Opportunity_Parent__c;
        //system.debug('### Param Key = ' + Opportunity_App__c.getAll().get('Opp_Param_Key').Opportunity_Parent__c);
        id = ApexPages.currentPage().getParameters().get(paramKey);
        returnUrl = ApexPages.currentPage().getParameters().get('retURL');
        //system.debug('### Opp Id = ' + id);
        Integer i;
        //This will add 3 default rows to insert 3 Critical Business Issues.
        for(i=1; i<=3; i++){
            BusinessIssueModel bim = new BusinessIssueModel();        
            bim.srNo = BusinessIssueModels.size() + 1;        
            bim.businessIssue = new CriticalBusinessIssue__c();        
            BusinessIssueModels.add(bim);
        }
    }
    
    //This function will add 3 more rows to add 3 new Business Issues.
    public PageReference AddIssue(){
        Integer i;
        for(i=1; i<=3; i++){
            BusinessIssueModel bim = new BusinessIssueModel();        
            bim.srNo = BusinessIssueModels.size() + 1;        
            bim.businessIssue = new CriticalBusinessIssue__c();        
            BusinessIssueModels.add(bim);
        }
        return null;
    }
    
    
    //This function will save each row as a new record in Business Issue for Selected Opportunity.
    public PageReference Save(){
        for(BusinessIssueModel bim: BusinessIssueModels){
            if(bim.businessIssue.body__c!= null){
                CriticalBusinessIssue__c businessIssue = new CriticalBusinessIssue__c();   
                if(bim.businessIssue.body__c.length() > 80){
                    
                    string myString = bim.businessIssue.body__c.substring(0, 77 );
                    businessIssue.Name = myString +'...';
                    system.debug('##### businessIssue.Name : '+businessIssue.Name);
                    businessIssue.body__c = bim.businessIssue.body__c;
                    businessIssue.Opportunity__c = id;
                    insert businessIssue;
                
                }else{
                
                    businessIssue.Name = bim.businessIssue.body__c;
                    businessIssue.body__c = bim.businessIssue.body__c;
                    businessIssue.Opportunity__c = id;
                    insert businessIssue;
                
                }     
                //businessIssue.Name = bim.businessIssue.Name;
                //businessIssue.Opportunity__c = id;
                //insert businessIssue;
            }
        }
        PageReference pg = new PageReference(returnUrl);
        return pg;    
    }
    
    class BusinessIssueModel{        
        public integer srNo {get;set;}        
        public CriticalBusinessIssue__c businessIssue{get;set;}    
    }
}
Amit Chaudhary 8Amit Chaudhary 8
Please try below code i hope that will help you
public class BusinessIssueController{
    public CriticalBusinessIssue__c businessIssue{get;set;}
    public LIST<CriticalBusinessIssue__c> businessIssueList{get;set;}
    public LIST<BusinessIssueModel> BusinessIssueModels{get;set;}
    public String id;
    public String returnUrl;
    //Constructor
    public BusinessIssueController(ApexPages.StandardController sc){
        businessIssue = new CriticalBusinessIssue__c();
        BusinessIssueModels = new LIST<BusinessIssueModel>();
        String paramKey = Opportunity_App__c.getAll().get('Opp_Param_Key').Opportunity_Parent__c;
        //system.debug('### Param Key = ' + Opportunity_App__c.getAll().get('Opp_Param_Key').Opportunity_Parent__c);
        id = ApexPages.currentPage().getParameters().get(paramKey);
        returnUrl = ApexPages.currentPage().getParameters().get('retURL');
        //system.debug('### Opp Id = ' + id);
        Integer i;
        //This will add 3 default rows to insert 3 Critical Business Issues.
        for(i=1; i<=3; i++){
            BusinessIssueModel bim = new BusinessIssueModel(); 
			if(BusinessIssueModels != null && BusinessIssueModels.size() >0 )
			{	
				bim.srNo = BusinessIssueModels.size() + 1;        
            }
			else
			{
				bim.srNo =  1;        
			}
			bim.businessIssue = new CriticalBusinessIssue__c();        
            BusinessIssueModels.add(bim);
        }
    }
    
    //This function will add 3 more rows to add 3 new Business Issues.
    public PageReference AddIssue(){
        Integer i;
        for(i=1; i<=3; i++){
            BusinessIssueModel bim = new BusinessIssueModel();        
			if(BusinessIssueModels != null && BusinessIssueModels.size() >0 )
			{	
				bim.srNo = BusinessIssueModels.size() + 1;        
            }
			else
			{
				bim.srNo =  1;        
			}
            bim.businessIssue = new CriticalBusinessIssue__c();        
            BusinessIssueModels.add(bim);
        }
        return null;
    }
    
    
    //This function will save each row as a new record in Business Issue for Selected Opportunity.
    public PageReference Save(){
        for(BusinessIssueModel bim: BusinessIssueModels){
            if(bim.businessIssue.body__c!= null){
                CriticalBusinessIssue__c businessIssue = new CriticalBusinessIssue__c();   
                if(bim.businessIssue.body__c.length() > 80){
                    
                    string myString = bim.businessIssue.body__c.substring(0, 77 );
                    businessIssue.Name = myString +'...';
                    system.debug('##### businessIssue.Name : '+businessIssue.Name);
                    businessIssue.body__c = bim.businessIssue.body__c;
                    businessIssue.Opportunity__c = id;
                    insert businessIssue;
                
                }else{
                
                    businessIssue.Name = bim.businessIssue.body__c;
                    businessIssue.body__c = bim.businessIssue.body__c;
                    businessIssue.Opportunity__c = id;
                    insert businessIssue;
                
                }     
                //businessIssue.Name = bim.businessIssue.Name;
                //businessIssue.Opportunity__c = id;
                //insert businessIssue;
            }
        }
        PageReference pg = new PageReference(returnUrl);
        return pg;    
    }
    
    class BusinessIssueModel{        
        public integer srNo {get;set;}        
        public CriticalBusinessIssue__c businessIssue{get;set;}    
    }
}
Please let us know if this will help you
 
Keenan WojniczKeenan Wojnicz
Thank you for your reply, I'm really needing to solve this issue. 
I used the code you provided for the BusinessIssueController but I'm still getting the null object error.

Any other ideas on what the problem could be? 
Abhishek BansalAbhishek Bansal
Hi Keenan,

I have done some modification in your controller class.
Please try with the below updated code :
public class BusinessIssueController{
    public CriticalBusinessIssue__c businessIssue{get;set;}
    public LIST<CriticalBusinessIssue__c> businessIssueList{get;set;}
    public LIST<BusinessIssueModel> BusinessIssueModels{get;set;}
    public String id;
    public String returnUrl;
    //Constructor
    public BusinessIssueController(ApexPages.StandardController sc){
        businessIssue = new CriticalBusinessIssue__c();
        BusinessIssueModels = new LIST<BusinessIssueModel>();
        String paramKey = Opportunity_App__c.getAll().get('Opp_Param_Key').Opportunity_Parent__c;
        //system.debug('### Param Key = ' + Opportunity_App__c.getAll().get('Opp_Param_Key').Opportunity_Parent__c);
        id = ApexPages.currentPage().getParameters().get(paramKey);
        returnUrl = ApexPages.currentPage().getParameters().get('retURL');
        //system.debug('### Opp Id = ' + id);
        Integer i;
        //This will add 3 default rows to insert 3 Critical Business Issues.
        for(i=1; i<=3; i++){
            BusinessIssueModel bim = new BusinessIssueModel();        
            bim.srNo = BusinessIssueModels.size() + 1;        
            bim.businessIssue = new CriticalBusinessIssue__c();        
            BusinessIssueModels.add(bim);
        }
    }
    
    //This function will add 3 more rows to add 3 new Business Issues.
    public PageReference AddIssue(){
        Integer i;
        for(i=1; i<=3; i++){
            BusinessIssueModel bim = new BusinessIssueModel();        
            bim.srNo = BusinessIssueModels.size() + 1;        
            bim.businessIssue = new CriticalBusinessIssue__c();        
            BusinessIssueModels.add(bim);
        }
        return null;
    }
    
    
    //This function will save each row as a new record in Business Issue for Selected Opportunity.
    public PageReference Save(){
        for(BusinessIssueModel bim: BusinessIssueModels){
            if(bim.businessIssue.body__c!= null){
                CriticalBusinessIssue__c businessIssue = new CriticalBusinessIssue__c();   
                if(bim.businessIssue.body__c.length() > 80){
                    
                    string myString = bim.businessIssue.body__c.substring(0, 77 );
                    businessIssue.Name = myString +'...';
                    system.debug('##### businessIssue.Name : '+businessIssue.Name);
                    businessIssue.body__c = bim.businessIssue.body__c;
                    businessIssue.Opportunity__c = id;
                    insert businessIssue;
                
                }else{
                
                    businessIssue.Name = bim.businessIssue.body__c;
                    businessIssue.body__c = bim.businessIssue.body__c;
                    businessIssue.Opportunity__c = id;
                    insert businessIssue;
                
                }     
                //businessIssue.Name = bim.businessIssue.Name;
                //businessIssue.Opportunity__c = id;
                //insert businessIssue;
            }
        }
        PageReference pg = new PageReference(returnUrl);
        return pg;    
    }
    
    class BusinessIssueModel{        
        public integer srNo {get;set;}        
        public CriticalBusinessIssue__c businessIssue{get;set;}    
		
		public BusinessIssueModel(){
			srNo = new Integer();
			businessIssue = new CriticalBusinessIssue__c();
		}
    }
}

Let me know if you still have any issue with this.

Thanks,
Abhishek Bansal.
Keenan WojniczKeenan Wojnicz
Hi Abhishek, 
Thanks for your response, when I used the code you provided for the BusinessIssueController I received:
Compile Error: Type cannot be constructed: Integer at line 75 column 20

I tried removing lines 74-76 but when I tried to preview my visualforce page I received the same null reference error.

Thanks,
Keenan
Abhishek BansalAbhishek Bansal
Hi Keenan,

I have update your code and it will work fine.
Please try with below code :
public class BusinessIssueController{
    public CriticalBusinessIssue__c businessIssue{get;set;}
    public LIST<CriticalBusinessIssue__c> businessIssueList{get;set;}
    public LIST<BusinessIssueModel> BusinessIssueModels{get;set;}
    public String id;
    public String returnUrl;
    //Constructor
    public BusinessIssueController(ApexPages.StandardController sc){
        businessIssue = new CriticalBusinessIssue__c();
        BusinessIssueModels = new LIST<BusinessIssueModel>();
        String paramKey = Opportunity_App__c.getAll().get('Opp_Param_Key').Opportunity_Parent__c;
        //system.debug('### Param Key = ' + Opportunity_App__c.getAll().get('Opp_Param_Key').Opportunity_Parent__c);
        id = ApexPages.currentPage().getParameters().get(paramKey);
        returnUrl = ApexPages.currentPage().getParameters().get('retURL');
        //system.debug('### Opp Id = ' + id);
        Integer i;
        //This will add 3 default rows to insert 3 Critical Business Issues.
        for(i=1; i<=3; i++){
            BusinessIssueModel bim = new BusinessIssueModel();        
            bim.srNo = BusinessIssueModels.size() + 1;        
            bim.businessIssue = new CriticalBusinessIssue__c();        
            BusinessIssueModels.add(bim);
        }
    }
    
    //This function will add 3 more rows to add 3 new Business Issues.
    public PageReference AddIssue(){
        Integer i;
        for(i=1; i<=3; i++){
            BusinessIssueModel bim = new BusinessIssueModel();        
            bim.srNo = BusinessIssueModels.size() + 1;        
            bim.businessIssue = new CriticalBusinessIssue__c();        
            BusinessIssueModels.add(bim);
        }
        return null;
    }
    
    
    //This function will save each row as a new record in Business Issue for Selected Opportunity.
    public PageReference Save(){
        for(BusinessIssueModel bim: BusinessIssueModels){
            if(bim.businessIssue.body__c!= null){
                CriticalBusinessIssue__c businessIssue = new CriticalBusinessIssue__c();   
                if(bim.businessIssue.body__c.length() > 80){
                    
                    string myString = bim.businessIssue.body__c.substring(0, 77 );
                    businessIssue.Name = myString +'...';
                    system.debug('##### businessIssue.Name : '+businessIssue.Name);
                    businessIssue.body__c = bim.businessIssue.body__c;
                    businessIssue.Opportunity__c = id;
                    insert businessIssue;
                
                }else{
                
                    businessIssue.Name = bim.businessIssue.body__c;
                    businessIssue.body__c = bim.businessIssue.body__c;
                    businessIssue.Opportunity__c = id;
                    insert businessIssue;
                
                }     
                //businessIssue.Name = bim.businessIssue.Name;
                //businessIssue.Opportunity__c = id;
                //insert businessIssue;
            }
        }
        PageReference pg = new PageReference(returnUrl);
        return pg;    
    }
    
    class BusinessIssueModel{        
        public integer srNo {get;set;}        
        public CriticalBusinessIssue__c businessIssue{get;set;}    
		
		public BusinessIssueModel(){
			srNo = 0;
			businessIssue = new CriticalBusinessIssue__c();
		}
    }
}

Please let me know if you still have any issue.

Thanks,
Abhishek Bansal.
Keenan WojniczKeenan Wojnicz
Hi Abhishek,

I was able to use the code for this controller but I'm still getting the de-reference a null object error.
Do you think the problem could be coming from my Visualforce page code that I referenced at the top? Or do you think the issue is in the controller code?

I'm just very confused as to why this is happening. I've used this code before In a different instance of SalesForce without any problems like this, but for some reason it's not working now..

Thanks,
Keenan
Abhishek BansalAbhishek Bansal
Hi Keenan,

I dont understand why the error is coming.
It would be good if you can contact me personally on my gmail Id or Skype so that i can look at your issue in a better way and will surely provide you help.
Gmail : abhibansal2790@gmail.com
SkypeId : abhishek.bansal2790

Let me know if you have any other way in which i can provide you help.

Thanks,
Abhishek
This was selected as the best answer