I have the below Visualforce page & controller class for a user to select a design document type & upload a file. Document_Type__c is a picklist and should be required.

The code below works except in 1 situation. If you choose a file and click upload, it uploads the file without confirming the Document Type has been selected. This gives the view state size limit error.

When changing line 8 to

if (attachment == null) 
   transient Attachment attachment = new Attachment();

this ensures the Document Type is selected first but when trying to upload a file, it returns the de-reference a null object error.

Any help would be greatly appreciated.

Visualforce page:

    <apex:page showHeader="false" sidebar="false" standardController="Case" extensions="CreateDesignDocumentExtensionController">

<apex:messages />
    <apex:form id="theForm">

        <apex:pageBlock >

            <apex:variable var="a" value="{!newDesignDoc}" />

            <apex:pageBlockSection columns="1">
                <apex:inputField label="Document Type" value="{!a.Document_Type__c}" required="true"/>
                <apex:inputFile value="{!attachment.body}" filename="{!attachment.name}"/>

            <apex:pageBlockButtons location="bottom">
                <apex:commandButton action="{!save}" value="Upload"/>



Apex controller:

    public with sharing class CreateDesignDocumentExtensionController {

    public final Case designCase;
    public Design__c newDesignDoc { get; set; }

    public Attachment attachment {
        get {
            if (attachment == null) attachment = new Attachment();
            return attachment;

    public CreateDesignDocumentExtensionController (ApexPages.StandardController stdController) {
        designCase = (Case)stdController.getRecord();
        newDesignDoc = new Design__c();

    public PageReference save() {       

        if (attachment.body == null) {
            ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Choose file to upload'));

        } else {

            try {
                newDesignDoc.Case__c = designCase.Id;
                insert newDesignDoc;
                attachment.parentId = newDesignDoc.Id;
                insert attachment;
            catch (DMLException e) {
                ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading file'));
                return null;
            finally {
                newDesignDoc.Document_Type__c = null;
                newDesignDoc = new Design__c();            
                attachment.body = null;
                attachment = new Attachment();

            ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'File uploaded successfully'));
            return null;

        PageReference page = ApexPages.currentPage();
        return page;
  • If all else fails you can check the value of document type via JS and return false if null preventing the command button action from executing: <apex:commandButton onclick="return validateType();" action="{!save}" value="Upload"/>
    – Eric
    Dec 22, 2016 at 1:18
  • i looked at some of my code that does uploads and I tend to bind the inputFile attributes to transient variables in the controller and then in the save page action method, instantiate an Attachment sobject from the transient variables - then insert.
    – cropredy
    Dec 22, 2016 at 1:47

1 Answer 1


I would try removing the "return null" statement in the Exception - I am wondering is that is preventing the finally from executing, and so the attachment never gets set back to new, and is being maintained in the viewstate?

  • 1
    A return is the only thing that would stop a finally block from occurring.
    – sfdcfox
    Dec 22, 2016 at 1:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .