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
Manish Anand 10Manish Anand 10 

Error:-Attempt to de-reference a null object. Error is in expression '{!Save}' in component <apex:commandButton> in page scenario4: Class.MyController.save

Hi,

I am trying to create an input component on VF page that would only allow pre defined formatting to be entered by the user.
First 3 chars should be from A-Z/a-z.Next 3 chars should be from 0-9.Next 3 chars can be alphanumeric.
I have written below 'MyController' and VF pages.
Public class MyController
{
 
  Public Account act {get;set;}
  public Account getaccount()
  {
   act= [Select ID, Name,Description FROM Account
             Where Id= :ApexPages.CurrentPage().getParameters().get('id')];
        if(Pattern.matches('[A-Za-z]{3}[0-9]{3}[a-zA-Z0-9]{3}', act.Description))
        return act;
        else
        {
          ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Error: Invalid Input.');
           ApexPages.addMessage(myMsg);
           return Null;
        }
   }
   
   Public PageReference save() 
   {
     update act;
     return Null;
   }
 }

VisualForce Page-
<apex:page controller="MyController">
<apex:form >
<apex:PageBlock >
<apex:inputField value="{!act.description}" />
<apex:commandButton action="{!Save}" value="Save new description" />
<apex:pageMessages />
</apex:PageBlock>
</apex:form>
</apex:page>
When I execute above VF, through an ID. I get a text field and by entering description, when I try to save it. It give me below error.
Attempt to de-reference a null object. Error is in expression '{!Save}' in component <apex:commandButton> in page scenario4: Class.MyController.save

 
Best Answer chosen by Manish Anand 10
Mahesh DMahesh D
Hi Manish,

Yes, both the above mentioned points are correct.

Please mark the post as resolved.

Regards,
Mahesh

All Answers

Rajiv Penagonda 12Rajiv Penagonda 12
Your member variable "act" is not initialized. You need to brush up on this (http://searchsoa.techtarget.com/definition/object-oriented-programming)

Add this constructor to your controller and your code should work:
public MyController() {
	act = getaccount();
}

 
Manish Anand 10Manish Anand 10
Hi Rajiv,

I tried this.It throws another exception: Script-thrown exception.
An unexpected error has occurred. Your solution provider has been notified. (System)

 
Mahesh DMahesh D
Hi Manish,

Please enable the debug and paste the debug error message so that it will be easy to troubleshoot the issue.

Regards,
Mahesh
Manish Anand 10Manish Anand 10
Hi Mahesh,

I have enabled debug for Apex Class.However, I don't see anything logged into after I run VF page, by passing an accound ID as a parameter in the url.I am very naive to Apex language, not sure, if I am using the correct way.

Regards,
Manish.
 
Manish Anand 10Manish Anand 10
Hi Mahesh,

I was able to generate debug logs.

36.0 APEX_CODE,FINEST;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO 20:51:28.3 (3960842)|EXECUTION_STARTED 20:51:28.3 (4002455)|CODE_UNIT_STARTED|[EXTERNAL]|06628000004Hf0K|VF: /apex/scenario4 20:51:28.3 (42352754)|PUSH_TRACE_FLAGS|[EXTERNAL]|01p28000007RmYE|MyController|APEX_CODE,FINEST;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO 20:51:28.3 (42380352)|CODE_UNIT_STARTED|[EXTERNAL]|01p28000007RmYE|MyController <init> 20:51:28.3 (42406607)|SYSTEM_MODE_ENTER|true 20:51:28.3 (43401106)|HEAP_ALLOCATE|[72]|Bytes:3 20:51:28.3 (43453723)|HEAP_ALLOCATE|[77]|Bytes:152 20:51:28.3 (43469594)|HEAP_ALLOCATE|[342]|Bytes:408 20:51:28.3 (43490032)|HEAP_ALLOCATE|[355]|Bytes:408 20:51:28.3 (43507655)|HEAP_ALLOCATE|[467]|Bytes:48 20:51:28.3 (43543537)|HEAP_ALLOCATE|[139]|Bytes:6 20:51:28.3 (43586737)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:7 20:51:28.3 (43616474)|METHOD_ENTRY|[1]|01p28000007RmYE|MyController.MyController() 20:51:28.3 (43625565)|STATEMENT_EXECUTE|[1] 20:51:28.3 (43636026)|STATEMENT_EXECUTE|[1] 20:51:28.3 (43650710)|METHOD_EXIT|[1]|MyController 20:51:28.3 (43683769)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8 20:51:28.3 (43704391)|VARIABLE_SCOPE_BEGIN|[5]|this|MyController|true|false 20:51:28.3 (43864032)|VARIABLE_ASSIGNMENT|[5]|this|{}|0x20c30a8 20:51:28.3 (43875208)|STATEMENT_EXECUTE|[1] 20:51:28.3 (43877315)|STATEMENT_EXECUTE|[4] 20:51:28.3 (43890208)|HEAP_ALLOCATE|[7]|Bytes:6 20:51:28.3 (43951431)|STATEMENT_EXECUTE|[6] 20:51:28.3 (43954962)|STATEMENT_EXECUTE|[7] 20:51:28.3 (43960333)|HEAP_ALLOCATE|[7]|Bytes:61 20:51:28.3 (43973598)|HEAP_ALLOCATE|[7]|Bytes:4 20:51:28.3 (43986229)|HEAP_ALLOCATE|[7]|Bytes:7 20:51:28.3 (44009551)|HEAP_ALLOCATE|[50]|Bytes:5 20:51:28.3 (44037618)|HEAP_ALLOCATE|[56]|Bytes:5 20:51:28.3 (44055433)|HEAP_ALLOCATE|[64]|Bytes:7 20:51:28.3 (44173105)|HEAP_ALLOCATE|[8]|Bytes:19 20:51:28.3 (44240573)|HEAP_ALLOCATE|[8]|Bytes:0 20:51:28.3 (44260174)|HEAP_ALLOCATE|[8]|Bytes:2 20:51:28.3 (44714035)|SOQL_EXECUTE_BEGIN|[7]|Aggregations:0|SELECT ID, Name, Description FROM Account WHERE Id = :tmpVar1 20:51:28.3 (50774961)|SOQL_EXECUTE_END|[7]|Rows:0 20:51:28.3 (50798353)|HEAP_ALLOCATE|[7]|Bytes:4 20:51:28.3 (50809260)|HEAP_ALLOCATE|[7]|Bytes:0 20:51:28.3 (50963107)|HEAP_ALLOCATE|[7]|Bytes:4 20:51:28.3 (50985358)|HEAP_ALLOCATE|[7]|Bytes:33 20:51:28.3 (51110324)|HEAP_ALLOCATE|[7]|Bytes:46 20:51:28.3 (51418076)|CODE_UNIT_FINISHED|MyController <init> 20:51:28.3 (51456795)|POP_TRACE_FLAGS|[EXTERNAL]|01p28000007RmYE|MyController|APEX_CODE,FINEST;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO
 
Mahesh DMahesh D
Hi Manish,

Please use the below controller code:
 
Public class MyController { 
  Public Account act {get;set;}
  public Account getAccount() {
   act= [Select ID, Name,Description FROM Account
             Where Id= :ApexPages.CurrentPage().getParameters().get('id')];
		if(act.Description != null) {
			if(Pattern.matches('[A-Za-z]{3}[0-9]{3}[a-zA-Z0-9]{3}', act.Description))
			return act;
			else
			{
			  ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Error: Invalid Input.');
			   ApexPages.addMessage(myMsg);			   
			}
		}
		return null;
   }
   
   Public PageReference save() 
   {
     update act;
     return null;
   }
   
   public  MyController() {
	   getAccount();
   }
}
Please do let me know if it helps.

Regards,
Mahesh

 
Manish Anand 10Manish Anand 10
Hi Mahesh,

Well, it doesn't throw an error or exception.What, I was missing?
Also, when I enter any description value and save for an account, nothing happens.

Regards,
Manish
Mahesh DMahesh D
Give the Description to the Account and it will display what you are expecting.

Regards,
Mahesh
Manish Anand 10Manish Anand 10
Mahesh,

When I enter Description through input box in the VF page, and try to save it, nothing happens.
When I enter incorrect format Description's value  through Account Detail page.It gets saved on the detail page and on VF Page, when I click save.It gives error.
When I enter correct format Description's value through Account Detail page,it gets saved on the detail page and on VF page, when I click save.
It still gives error.

My requirement is to update Description on the account detail, through input field on VF page and it should give error if it's in the incoorect format
and should not get saved.If value is in the correct format, it should be saved, without any error.

Regards,
Manish
 
Mahesh DMahesh D
Hi Manish,

Please find the final code:
 
Public class MyController {
    Public Account act {get;set;}
    public Account getAccount() {
        act= [Select ID, Name,Description FROM Account
                 Where Id= :ApexPages.CurrentPage().getParameters().get('id')];
        if(act.Description != null) {
            if(Pattern.matches('[A-Za-z]{3}[0-9]{3}[a-zA-Z0-9]{3}', act.Description))
                return act;
            else {
                ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Error: Invalid Input.');
                ApexPages.addMessage(myMsg);            
            }
        }
        return null;
    }
   
    Public PageReference save() {
        if(act.Description != null) {
            if(Pattern.matches('[A-Za-z]{3}[0-9]{3}[a-zA-Z0-9]{3}', act.Description)) {
                update act;
                return new PageReference('/'+act.Id);
            } else {
                ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Error: Invalid Input.');
                ApexPages.addMessage(myMsg);
            }
        }
        return null;
    }
   
    public  MyController() {
        getAccount();
    }
}

I also tested the above code and it looks good now.

Please do let me know if it helps you.

Regards,
Mahesh
Manish Anand 10Manish Anand 10
Mahesh,

It's still same.Do you think this issue can be due to VF page?
Below is VF page:-

<apex:page controller="MyController" tabStyle="Account">
<apex:form >
<apex:PageBlock >
<apex:inputField value="{!account.description}" />
<apex:commandButton action="{!Save}" value="Save new description" />
<apex:pageMessages />
</apex:PageBlock>
</apex:form>
</apex:page>
Mahesh DMahesh D
Remove the pageblock tag
Manish Anand 10Manish Anand 10
Still no luck buddy.
Mahesh DMahesh D
What is the error you are getting.

Also paste your latest VF and controller.

I tested in my DE org and it is working properly.

Regards,
Mahesh
Mahesh DMahesh D
Hi Manish,

Did you get a chance to test the solution which I provided in the latest reply.

Regards,
Mahesh
Manish Anand 10Manish Anand 10
Hi Mahesh,
Below is my latest controller class and VF page.
Public class MyController {
    Public Account act {get;set;}
    public Account getAccount() {
        act= [Select ID, Name,Description FROM Account
                 Where Id= :ApexPages.CurrentPage().getParameters().get('id')];
              return act;
    }
   Public PageReference save() {
     if (act.Description != Null)
     {
         if( Pattern.matches('[A-Za-z]{3}[0-9]{3}[a-zA-Z0-9]{3}', act.Description))
         {
            update act; 
         }
         else
         {
           ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Error: Invalid Format.');
          }
     }
       return Null;
   }
   
 }
VF page:-
<apex:page controller="MyController">
<apex:form >
<apex:inputText value="{!Account.description}" />
<apex:commandButton value="Save new description" action="{!Save}" />
<apex:pageMessages />
</apex:form>
</apex:page>
When I run above VF in the browser, passing an account id as a parameter. I get a text field with a button-Save new description.
Issue- Whatever, I enter in the input text field is not passed to controller class and hence desctiption value is not updated.
Example-Currently if value of Description is 'testing scenario 4' and in the blank input text box, I enter lets say 'testing' and click on button.
Nothing happens. Page doesn't reload. And when I check value of description by going onto account detal page, through tab, it's value is
still 'testing scenario4 4'

So, how do we pass value in entered in the input text to controller class?

Regards,
Manish.

 
Manish Anand 10Manish Anand 10
Hi Mahesh,
I got this fixed.Thanks to wonderful Developer forum. :-)
To pass the value of Input Field in the Apex code, I used public string property in the controller class.
I concluded couple of points:-
1)Earlier, I used value attribute in the Input Field as Sobject, but it should be an instance of object or a primitive data type, that's what I realized.
2)Another takeway, Every custome Controller must have either a public property (usually to get values from VF page to the controller class) or a public getXXX method (usually to get values from VF page to the controller class) and perform any operation.

Please let me know, if above points are true and my understanding is clear.

Regards,
Manish.
Mahesh DMahesh D
Hi Manish,

Yes, both the above mentioned points are correct.

Please mark the post as resolved.

Regards,
Mahesh
This was selected as the best answer