0

I've two vf pages QRCase (output) and QRCaseEdit(input/Edit) which are used to create cases for a specific record type 'Quality Report Case'. I tried writing the test class for controller, but it's failing. I'm getting an

Error: "System.NullPointerException: Attempt to de-reference a null object" . This is stack trace: Class.QRreportController.upload: line 81, column 1 Class.TestQRcasecontroller.TestQRreportController: line 46, column 1

When I remove line 46 from test class, 'controller.upload();' and run it, I'm getting around 63% code coverage. Line 81 from above error refers to : attachment.OwnerId = UserInfo.getUserId();

What am I doing wrong. This is my first attempt at the test class. Please guide me. Thank you!

    **VF PAGE CONTROLLER** 

   public class QRreportController
   {
    private ApexPages.StandardController std;        

    public Case c {get; set;}


    public QRreportController (ApexPages.StandardController stdCtrl)
    {
        // prepopulates some fields on page load
        this.c = (Case)stdCtrl.getRecord();
        Map<ID,Schema.RecordTypeInfo> rt_Map = 
      Case.sObjectType.getDescribe().getRecordTypeInfosById();
        Id RecordTypeIdCase = 
     Schema.SObjectType.Case.getRecordTypeInfosByName().get('Quality Report 
      Case').getRecordTypeId();
        Id rtId = RecordTypeIdCase;
        c.RecordTypeId=rtId;



c.AccountId=System.currentPageReference().getParameters().get('AccountId');
        std=stdCtrl;
    }


/* --------------------- Get Case ------------------------------- */     
    public Case getCase()
    {       
     return (Case) std.getRecord(); 
    }

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

/* --------------------- Save and Exit method ------------------------------- */
    public PageReference saveAndExit()
    {
        std.save();
        PageReference pageRef1 = new PageReference('/' + getCase().id);
        return pageRef1;

    }
/* --------------------- Edit method ------------------------------- */
    public PageReference edit1()
    {
        PageReference pageRef2 = Page.QRcaseEdit;
        pageRef2.getParameters().put('id', getCase().id);
        pageRef2.getParameters().put('AccountId', getCase().AccountId);
        return pageRef2;
    }


/* --------------------- Quick Save method ------------------------------- */
public PageReference save()
    {
        std.save();
        PageReference pageRef3 = Page.QRcase;
       ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'SUCCESS!  Changes saved.'));

        pageRef3.getParameters().put('id', getCase().id);
        pageRef3.getParameters().put('AccountId',getCase().AccountId);
        return pageRef3;

    }

    public PageReference upload() {

    attachment.OwnerId = UserInfo.getUserId();
    attachment.ParentId = getCase().Id ;
    attachment.IsPrivate = true;

    try {
      insert attachment;
    } catch (DMLException e) {
      ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading attachment'));
      return null;
    } finally {
      attachment = new Attachment(); 
    }

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


   }

Below is the Testclass:

@isTest

public class TestQRcasecontroller {

static testMethod void TestQRreportController () {


Id rtId = [select Id, name from RecordType where name = 'Quality Report 
Case' and SObjectType = 'Case' limit 1].Id;


//create account
//
Account acc = new Account();
acc.Name = 'Test Account';
insert acc;


Id RecordTypeIdCase = 
Schema.SObjectType.Case.getRecordTypeInfosByName().get('Quality Report 
Case').getRecordTypeId();

//create case
Case c = new Case();
//enter details
c.AccountId = acc.Id;
c.RecordTypeId = RecordTypeIdCase;
c.Site_State__c = 'New York';
c.Product_Serial_Number__c = '91-4154';
c.Description = 'Broken filter';
c.QR_Action_Taken__c = 'Basic troubleshoot';    
c.QR_Problem_Type__c = 'Poor beverage taste';
c.QR_Component_Type__c = 'Filter';
c.QR_Occurrence_Rate__c = 'Daily';
c.QR_Severity__c = 'Operates Intermittently';
insert c;


                PageReference pageRef = Page.QRCaseEdit;
pageRef.getParameters().put('Id',c.Id);
pageRef.getParameters().put('AccountId',acc.Id);
Test.setCurrentPageReference(pageRef);

                PageReference pageRef1 = Page.QRCase;
pageRef1.getParameters().put('Id',c.Id);
pageRef1.getParameters().put('AccountId',acc.Id);
Test.setCurrentPageReference(pageRef1);
 ApexPages.StandardController sc = new ApexPages.StandardController(c);
                QRreportController controller = new QRreportController(sc);

                controller.getCase();
                controller.saveAndExit();
                controller.upload();
                controller.edit1();
                controller.save();
                controller.getCase();

}

}
1

Your property attachment is hard-coded to return null.

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

This guarantees that any time you refer to any property of attachment, you will get a NullPointerException. You should return attachment instead of null.

Additionally, you're setting yourself up for confusing errors by using the name of a system object as a property - remember that Apex is case-insensitive. Best to change the name of the property.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy