You need to sign in to do that
Don't have an account?
Error : Attempt to de-reference a null object
I need help again. I am still new at this and I don't fully understand Apex.
I wrote a vp for a user to create a new record. Here
I am not getting any errors when editing the page, but when I run it I get the following error:
Attempt to de-reference a null object
An unexpected error has occurred. Your development organization has been notified.
Here is my code:
<apex:page standardController="Case" showHeader="true" sidebar="true" extensions="CaseEditExtension">
<apex:form >
<apex:sectionHeader title="Visualforce Form" subtitle="Create a Request"/>
<apex:pageMessages />
<apex:pageBlock title="Welcome {!$User.FirstName}!" id="pgBlock" mode="edit">
<apex:pageBlockButtons location="both">
<apex:commandButton value="Save" action="{!Save}" />
<apex:commandButton value="Cancel" action="{!Cancel}"/>
</apex:pageBlockButtons>
<apex:pageBlock title="Contact Info">
<apex:outputLabel >Account Name</apex:outputLabel>
<apex:outputField value="{!case.AccountId}" />
</apex:pageBlock>
<apex:pageBlockSection id="pgBlockSectionDescriptionInfo" title="Description Info" collapsible="false" columns="2">
<apex:pageBlockSectionItem >
<apex:outputLabel >Subject</apex:outputLabel>
<apex:inputText id="caseSubject" value="{!case.Subject}" size="75" />
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem ></apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputLabel >Description</apex:outputLabel>
<apex:inputTextArea id="caseDescription" value="{!case.Description}" cols="75" rows="6" />
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem ></apex:pageBlockSectionItem>
</apex:pageBlockSection>
<apex:pageBlockSection id="cas5" title="Case Detail:" collapsible="false" columns="2">
<apex:pageBlockSectionItem >
<apex:outputLabel >Type</apex:outputLabel>
<apex:inputField id="caseType" value="{!case.Type}" />
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem ></apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputLabel >Case Reason</apex:outputLabel>
<apex:inputField id="caseReason" value="{!case.Reason}" />
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem ></apex:pageBlockSectionItem>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
Thank you for your help.
I think error is here
if(Comment.Length() > 0)
as you don't instanciate variable.
put Comment="" into "public CaseEditExtension(ApexPages.StandardController con)" function.
All Answers
Hello,
post CaseEditExtension class (there is error)
Here is CaseEditExtension.
I did not write this, but It's used in other pages where it works OK.
Thanks,
Haya
public with sharing class CaseEditExtension {
public String Comment
{
get;
set;
}
public String Description
{
get;
set;
}
public String Subject
{
get;
set;
}
public Boolean UseAssignmentRules
{
get;set;
}
public Case cs;
ApexPages.StandardController controller;
public CaseEditExtension(ApexPages.StandardController con)
{
controller = con;
this.cs = (Case) con.getRecord();
}
public PageReference Save()
{
CaseComment com = new CaseComment();
if(Comment.Length() > 0)
{
com.commentBody = Comment;
com.ParentId = cs.Id;
if(UseAssignmentRules == true)
{
AssignmentRule AR = new AssignmentRule();
AR = [select id from AssignmentRule where SobjectType = 'Case' and Active = true limit 1];
//Creating the DMLOptions for "Assign using active assignment rules" checkbox
Database.DMLOptions dmlOpts = new Database.DMLOptions();
dmlOpts.assignmentRuleHeader.assignmentRuleId= AR.id;
controller.getRecord().setOptions(dmlOpts);
}
insert com;
}
String retURL = ApexPages.currentPage().getParameters().get('retURL');
String CurrentId = ApexPages.currentPage().getParameters().get('id');
PageReference redirectPG;
if(retURL != null)
redirectPG = new PageReference('/' + retURL);
else if(CurrentId != null)
redirectPG = new PageReference('/' + CurrentId);
controller.Save();
return Page.Request_Create_Thankyou;
}
public PageReference SaveAndNew()
{
Save();
return new PageReference('/500/e');
}
public PageReference SaveAndClose()
{
Save();
String retURL = ApexPages.currentPage().getParameters().get('retURL');
String CurrentId = ApexPages.currentPage().getParameters().get('id');
PageReference redirectPG;
if(retURL != null)
redirectPG = new PageReference('/' + retURL);
else if(CurrentId != null)
redirectPG = new PageReference('/' + CurrentId);
return redirectPG;
}
}
I think error is here
if(Comment.Length() > 0)
as you don't instanciate variable.
put Comment="" into "public CaseEditExtension(ApexPages.StandardController con)" function.
If I understand you correctly I think you are telling me to edit CaseEditExtension. I am not sure I can do that since it's being used in other instances.
Is there a way to fix the problem in the visuaforce page?
Hello,
yes, in the class CaseEditExtension.
Why it works on other VF pages? Because comment is probbabily used there and is therefore instanciated in VF page. I don't see any Comment usage in your VF page. -> So yes, you may also solve the problem in VF page. See, how "{!comment}" is used on other VF pages.
You were right. The problem was with the comment.
I decided to go this route in the VF page:
<apex:inputHidden value="{!Comment}" id="theHiddenInput"/>
Thank you so much.