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
Salesforce Test 5Salesforce Test 5 

Error while uploading

I have created a apex class for uploading file.but i am uplaoding any files its showing this to achieve this error ?

System.NullPointerException: Attempt to de-reference a null object
Error is in expression '{!doUploadFile}' in component <apex:commandButton> in page scoupload: Class.SCOUpload.doUploadFile: line 74, column 1
Class.SCOUpload.doUploadFile: line 74, column 1
public with sharing class SCOUpload {
    *   attachment
    public Purchase_Order__c purchaseOrder{get;set;}
 Public  Transient Attachment tAttachment{get;set;}
    public boolean showC1RecordType {get;set;}
    public boolean showC2RecordType {get;set;} 
    private Id c1RecordTypeId; 
    private Id c2RecordTypeId;
    *   constructor
    public SCOUpload(ApexPages.StandardController controller)
        //initialize attachment container
        this.tAttachment = new Attachment();
        c1RecordTypeId = Schema.SObjectType.Purchase_Order__c.getRecordTypeInfosByName().get('C1').getRecordTypeId();
        c2RecordTypeId = Schema.SObjectType.Purchase_Order__c.getRecordTypeInfosByName().get('C2').getRecordTypeId();
        purchaseOrder = new Purchase_Order__c();
        purchaseOrder = (Purchase_Order__c)controller.getRecord();
        String isButtonClicked = Apexpages.currentPage().getParameters().get('setDefaultValues');
        if(isButtonClicked == 'true'){
        showC1RecordType = false;
        showC2RecordType = false;
        tAttachment= new Attachment();
        if(purchaseOrder.RecordTypeId == c1RecordTypeId){
            showC1RecordType = true;
            showC2RecordType = false;
        else if(purchaseOrder.RecordTypeId == c2RecordTypeId){
            showC1RecordType = false;
            showC2RecordType = true;
     public void setDefaultValues(){
        String quoteId = Apexpages.currentPage().getParameters().get('quoteId');
        Quote__c quote = [Select,, o.Quantity_formula__c, o.Opportunity_Product_Detail__r.Opportunity__c,
                    o.Opportunity__c, o.Record_type_name__c,o.Company_Name__c,Opportunity_Product_Detail__r.Opportunity__r.Account__c from Quote__c o WHERE];
        purchaseOrder.Opportunity__c = quote.Opportunity_Product_Detail__r.Opportunity__c;
        purchaseOrder.Quote__c =;
        purchaseOrder.Company__c = quote.Opportunity_Product_Detail__r.Opportunity__r.Account__c;
        if(quote.Record_type_name__c == 'C1'){
            purchaseOrder.RecordTypeId = c1RecordTypeId;
        else {
            purchaseOrder.RecordTypeId = c2RecordTypeId;
    *   upload file
    public PageReference doUploadFile(){
        if(tAttachment.Body == null){
            Apexpages.addMessage(new Apexpages.Message(Apexpages.Severity.ERROR, 'Please upload recent Purchase Order  before saving the record'));
            return null;
                insert purchaseOrder;
              tAttachment.OwnerId = UserInfo.getUserId();
               tAttachment.ParentId =;
              insert tAttachment;
                return new PageReference('/';
            catch(DMLException e){
                ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,e.getMessage()));
                return null;

Tavva Sai KrishnaTavva Sai Krishna
Hi Test 5,

As i see, here you have commented the controller where you have intialised the "tAttachment " variable. so uncomment the controller and check try again. 

Let me know ,if you have any doubts are still face any errors.

Waiting for your reply,

Sai Krishna Tavva.
Salesforce Test 5Salesforce Test 5
Hi Sai Krishna We are facing same issue...have any idea ? Regards Maheswar