I am trying to add a attachment to a record. Their may or may not be attachment. So that i have created a attachment in the controller if the attachment is null but i am getting null point exception. EXCEPTION_THROWN|[74]|System.NullPointerException: Attempt to de-reference a null object
my code
<apex:form >
<apex:pageBlock >
<apex:pageBlockSection columns="6" >
<apex:pageBlockSectionItem >
<apex:outputLabel value="" for="fileInput" />
<apex:inputFile value="{!Attachment.body}" id="fileInput" filename=" {!Attachment.name}" dir="RTL" accept=".xlsx,.pdf,.docx" size="50" />
</apex:pageBlockSectionItem>
public Attachment attachment;
public Attachment getAttachment()
{
system.debug('Result : In attachment get () ');
if(attachment == null)
attachment = new Attachment();
return attachment;
}
if(exp.Receipt_Attached__c == true && attachment.body == null)
{
ApexPages.Message attMsg = new ApexPages.Message(ApexPages.severity.ERROR,'Expense Receipt is Required.');
ApexPages.addMessage(attMsg);
return null;
}
system.debug('Result : attachment '+ attachment);
if( attachment.body != null)
{
exp.Receipt_Attached__c=true;
}
I am getting error on this line of code if( attachment.body != null).