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
ChinnoduChinnodu 

System.NullPointerException: Attempt to de-reference a null object , Error is in expression '{!doAttach}' in component <apex:commandButton> in page gbocase: Class.gboCaseController.doAttach: line 316, column 1

HI Apex EXPERTSxperts,

I got Below error on Apex class as well as i have attached with my class , Could you please help me

System.NullPointerException: Attempt to de-reference a null object
Error is in expression '{!doAttach}' in component <apex:commandButton> in page gbocase: Class.gboCaseController.doAttach: line 316, column 1
Class.gboCaseController.doAttach: line 316, column 1.
public class gboCaseController {
Public Transient BLOB attachmentBody{get; set;}        
Public String attachmentName{get; set;}
Public Attachment objCaseAtt {get; set;}
public String caseId{get; set;} 
 
Public List<Attachment> lst_attch{get; set;} 
        
    
    public gboCaseController(ApexPages.StandardController controller) {
       objCaseAtt = new Attachment(); 
    
    caseId = ApexPages.CurrentPage().getParameters().get('id');
    }
    
    public Case c { get; set; }
    
    /*Public string str {get; set;}
public List<SelectOption> getTypeOptions() 
{
List<SelectOption> options = new List<SelectOption>();
String Opt = '';
List<String> strList = new List<String>();
List<Channel_Type__c> listVals = Channel_Type__c.getAll().values();
For(Channel_Type__c ChnlType: listVals )
{
if(ChnlType.Case_Type__c=='Channel')
{
Opt =  ChnlType.Record_Type__c;
strList.Add(Opt);      
}
}
strList.sort();
options.add(new SelectOption('','--None--'));
for(String str : strList)
{
options.add(new SelectOption(str,str));
}
return options;
}*/
    
    //CaseVO caseVO = new CaseVO();
    
    //  public CaseRecordType caseRecordType ;
    //   
    /*  public void setCaseVO (CaseVO caseVO ) 
{
caseVO = caseVO;
}
public CaseVO getCaseVO () 
{
return caseVO ;
}   */
    
    public gboCaseController() {
        //c = new Case(RecordTypeId = '012560000000DrN');
        Id recTypeId = [Select Id, Name from RecordType where Name = 'GBOCase'][0].Id;
        c = new Case(RecordTypeId = recTypeId);
        c.Priority = 'Medium';
        //c.Record_Type__C = 'GBOcase';
      
        
    }
    public PageReference submitCase() {
        boolean isMandatoryFailed = false;
        if(c.SuppliedName== null || c.SuppliedName==''){
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error,'Please Enter Contact Name'));
            isMandatoryFailed = true;   
            
        }
        if(c.SuppliedCompany== null || c.SuppliedCompany==''){
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error,'Please Enter Company'));   
            isMandatoryFailed = true;   
            
        }
        if(c.SuppliedEmail== null || c.SuppliedEmail=='' ){
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error,'Please Enter Email'));   
            isMandatoryFailed = true;   
            
        }
        if(c.suppliedEmail.contains('@gmail.com')|| c.suppliedEmail.contains('@yahoo.com')|| c.suppliedEmail.contains('@hotmail.com')){
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error,'Please enter valid business email'));   
            isMandatoryFailed = true;   
            
        }
        if(c.SuppliedPhone== null || c.SuppliedPhone==''){
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error,'Please Enter Phone'));   
            isMandatoryFailed = true;   
            
        }
        if(c.To__c== null || c.To__c==''){
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error,'Please pick up a value from To'));   
            isMandatoryFailed = true;   
        }
        
        if(c.GBO_Type__c== null || c.GBO_Type__c==''){
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error,'Please pick up a value from GBOType'));   
            isMandatoryFailed = true;   
        }
        if(c.Subject== null || c.Subject==''){
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error,'Please Enter Subject'));   
            isMandatoryFailed = true;
        }
        if(c.Description== null || c.Description==''){
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error,'Please Enter Description'));   
            isMandatoryFailed = true;
        }
        if(c.Country__c== null || c.Country__c==''){
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error,'Please Enter Country'));   
            isMandatoryFailed = true;
        }
        
        if(isMandatoryFailed){
            return null;
        }
        List<String> contList = new List<String>(); 
        try {
            Integer i=0;        
            // now look for an associated contact with the same email
            for(Contact cnt : [SELECT Id,Email FROM Contact WHERE Email = :c.SuppliedEmail]){
                if (cnt != null) {
                    contList.add(cnt.Id);
                    i++;
                }
            }
            If(contList!=null){
                if(i<1){
                    
                }else if(i>1){
                    
                }else if (i==1){
                    for(String contStr :contList)
                    {
                        c.ContactId=contStr;
                    }
                    
                }
            }
            
            
            // c.RecordTypeId=c.Record_Type__c;
            // Specify DML options to ensure the assignment rules are executed
            Database.DMLOptions dmlOpts = new Database.DMLOptions();
            dmlOpts.assignmentRuleHeader.useDefaultRule = true;
            c.setOptions(dmlOpts);
            c.Is_Submitted_From_Site__c = true;
            c.IsVisibleInSelfService = False; 
            // Insert the case
            INSERT c;
            PageReference redirectPage= new PageReference('https://www.rsa.com');
            return redirectPage;//new PageReference('/thanks');
        } catch (Exception e) {
            ApexPages.addMessages(e);
            System.debug('Exception in gboCaseController: '+e);
            return null;
        }
    }
    public PageReference submitCaseWithAttachment() {
        
        boolean isMandatoryFailed = false;
        if(c.SuppliedName== null || c.SuppliedName==''){
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error,'Please Enter Contact Name'));
            isMandatoryFailed = true;   
            
        }
        if(c.SuppliedCompany== null || c.SuppliedCompany==''){
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error,'Please Enter Company'));   
            isMandatoryFailed = true;   
            
        }
        if(c.SuppliedEmail== null || c.SuppliedEmail==''){
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error,'Please Enter Email'));   
            isMandatoryFailed = true;   
            
        }
        if(c.suppliedEmail.contains('@gmail.com')|| c.suppliedEmail.contains('@yahoo.com')|| c.suppliedEmail.contains('@hotmail.com')){
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error,'Please enter valid business email'));   
            isMandatoryFailed = true;   
            
        }
        if(c.SuppliedPhone== null || c.SuppliedPhone==''){
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error,'Please Enter Phone'));   
            isMandatoryFailed = true;   
            
        }
        if(c.To__c== null || c.To__c==''){
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error,'Please pick up a value from To'));   
            isMandatoryFailed = true;   
        }
        if(c.GBO_Type__c== null || c.GBO_Type__c==''){
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error,'Please pick up a value from GBOType'));   
            isMandatoryFailed = true;   
        }
        if(c.Subject== null || c.Subject==''){
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error,'Please Enter Subject'));   
            isMandatoryFailed = true;
        }
        if(c.Description== null || c.Description==''){
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error,'Please Enter Description'));   
            isMandatoryFailed = true;
        }
        if(c.Country__c== null || c.Country__c==''){
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error,'Please Enter Country'));   
            isMandatoryFailed = true;
        }
        
        if(isMandatoryFailed){
            return null;
        }
        List<String> contList = new List<String>(); 
        try {
            Integer i=0;        
            // now look for an associated contact with the same email
            for(Contact cnt : [SELECT Id,Email FROM Contact WHERE Email = :c.SuppliedEmail]){
                if (cnt != null) {
                    contList.add(cnt.Id);
                    i++;
                }
            }
            If(contList!=null){
                if(i<1){
                    
                }else if(i>1){
                    
                }else if (i==1){
                    for(String contStr :contList)
                    {
                        c.ContactId=contStr;
                    }
                    
                }
            }
            //   c.Type=CaseVO.getType();
            
            // c.RecordTypeId=c.Record_Type__c;
            // Specify DML options to ensure the assignment rules are executed
            Database.DMLOptions dmlOpts = new Database.DMLOptions();
            dmlOpts.assignmentRuleHeader.useDefaultRule = true;
            c.setOptions(dmlOpts);
            c.Is_Submitted_From_Site__c = true;
            // Insert the case
            // INSERT c;
            // Added by Kavitha - Webtocase Attachment - Start               
            Database.SaveResult res = Database.Insert(c,false);
            System.debug('gboCaseController Error: '+res.getErrors());
            PageReference redirectAttachmentPage;
            if(res.getId()!= null)
            {
                case cs = [select id,CaseNumber from case where id =:res.getId()];
                redirectAttachmentPage= new PageReference('/apex/SubmitCaseAttachmentPage?id='+res.getId());
                // redirectAttachmentPage= new PageReference('http://sit-rsa.cs1.force.com/webtocase/p/attach/NoteAttach?pid='+cs.id+'&parentname='+cs.casenumber);
                redirectAttachmentPage.setRedirect(TRUE);
                return redirectAttachmentPage;//new PageReference('/thanks');
            }else
            {
                String strError; 
                for(Database.Error err : res.getErrors()) 
                {
                    strError += err+ ';';
                }
                
                return null;
                
            }
        } catch (Exception e) {
            ApexPages.addMessages(e);
            return null;
        }
    }
    
    //added by viswa from 271 line to 297 line
    
    public List<SelectOption> getPickLstValue()

    {

    List<SelectOption> options = new List<SelectOption>();

     Schema.DescribeFieldResult fieldResult = Case.Priority.getDescribe();

    List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();

    for(Schema.PicklistEntry f : ple)

    {

     if(f.getValue() != 'Hot')

     {

   options.add(new SelectOption(f.getLabel(), f.getValue()));

   }

     }  

           return options;            

  }

    
    public void doAttach()
     {

        String caseid1 = ApexPages.CurrentPage().getParameters().get('id');

       
 
        if(attachmentName == null && attachmentBody == null)        
        {
          ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error: Please specify a file to upload. Type in the path to the file, or use the "Browse" button to locate it in your local filesystem.'));
        }
        else        
        { 
       
         objCaseAtt.Name     = attachmentName;            
          objCaseAtt.Body     = attachmentBody;           
          objCaseAtt.ParentId = caseId;  
            try{             
          insert(objCaseAtt); 
          }
          catch(exception e){
          ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error: Please specify a file to upload. Type in the path to the file, or use the "Browse" button to locate it in your local filesystem.'));
          }
          finally{
         objCaseAtt = new attachment();
         
          } 
          
      /*  Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
        efa.setFileName(attachmentName);
        efa.setContentType('application/Pdf');
        efa.setBody(attachmentBody);  
        system.debug('Test---90>>>'+attachmentBody.size());
       // if(attachmentBody.size()<156000) {          
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.setUseSignature(false);
        mail.setToAddresses( new String[] {System.Label.webtocaseattachinsert});
     //  mail.setToAddresses('udaykumar.pathi@rsa.com');
        mail.setSubject(caseId);
        mail.setPlainTextBody('case attachment');
        mail.setFileAttachments(new Messaging.EmailFileAttachment[] { efa }); 
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });*/
      //  }
       /* else{
         ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error: Size of the file cannot be greater than 150KB.'));*/     

       //}
            lst_attch = new List<attachment>();
            lst_attch = [Select id,name,Parent.name from attachment where parentid =:caseid];
          }
        }
    
}
Regards,
VIswa
 
Magesh Mani YadavMagesh Mani Yadav
Hi Vishwa,

Update you code from line number 309 as
if(attachmentName != null && attachmentBody != null)        
        {
objCaseAtt.Name     = attachmentName;            
          objCaseAtt.Body     = attachmentBody;           
          objCaseAtt.ParentId = caseId;  
            try{             
          insert(objCaseAtt); 
          }
          catch(exception e){
          ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error: Please specify a file to upload. Type in the path to the file, or use the "Browse" button to locate it in your local filesystem.'));
          }
          finally{
         objCaseAtt = new attachment();
         
          }
          
        }
        else        
        { 
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error: Please specify a file to upload. Type in the path to the file, or use the "Browse" button to locate it in your local filesystem.'));
}


 
ChinnoduChinnodu
HI Magesh Mani Yadav,

Thank you for your reply, But now it's not working .

Thanks,
Viswa 
 
Magesh Mani YadavMagesh Mani Yadav
What error r u getting now?
Magesh Mani YadavMagesh Mani Yadav
any update on this?