This is my main class when i wrote test class for this class i got 41%.i need 75% code coverage for this class. Please check it..

-----Main class-------

public with sharing class AccAndCons {

  public  AccAndCons (){}
  public Account acc{get;set;}

  public AccAndCons(ApexPages.StandardController controller) {
     index = new List<integer>();
      List<contact>    listCon  = new List<contact>();
     for(integer i=0;i<=1;i++){
          listCon.add(new Contact());


  public List<integer> index { get; set; }

  public List<contact> listCon { get; set; }

  public PageReference cancel1(){       
    PageReference pg =new PageReference(' https://ap1.salesforce.com/001/o');
    return pg;    

  public PageReference save() {
    list<contact> cc=new list<contact>();
    Account acc= new Account();
    Database.SaveResult srList = Database.insert(acc);
        System.debug('list of Contacts'+listCon);
        for(contact c:listCon ){
        System.debug('insert account'+acc);
        // insert acc;
        insert cc;
     PageReference pg =new PageReference(' https://ap1.salesforce.com/'+acc.id);


     return pg  ;                       
    public String name { get; set; }


@isTest (seeAllData=false)
 public class AccAndCons_Test{

    static testMethod void AccAndCons_testMethod(){

    string name='xyz';
     Account a =new Account();
          insert a; 
     contact c1=new contact(lastName='test1',AccountId=a.id);
     contact c2=new contact(lastName='test2',AccountId=a.id);
     contact[] con =new contact[]{c1,c2};


    AccAndCons aa=new AccAndCons();




Thank You.

  • 1
    In the developer console you can see your lines not covered marked in red in test class. This will help you to create a proper test class
    – Sonal
    Jul 3 '14 at 6:04
  • Does this code actually do what you want it to do? Apart from that, you have no asserts in your test class, so what are you actually testing?
    – Nick Cook
    Jul 3 '14 at 6:29
  • Hi,when i debug this test class it show sume error like:System.NullPointerException: Attempt to de-reference a null object and Class.TestPackage121.AccAndCons.save: line 35, column 1 Class.TestPackage121.AccAndCons_Test.AccAndCons_testMethod: line 31, column 1 Jul 3 '14 at 8:41

I was going to make this a comment, but there wasn't room to fit it all in.

Its difficult to tell which lines are 35 and 31, but I think the error may be telling you that insert cc; is empty. You may want to wrap that as follows:

if(cc.isEmpty() == false){ 
   `insert cc;`

That way it won't try to do an insert records if list cc has no records and there won't be a null pointer reference.

I also noticed you had this at the bottom of your class:

public String name { get; set; }

First, getters and setters should be located at the beginning of a class. More importantly, variables like name should NOT be used as a getter or setter. They can be confused later when in entered in statements like this one:


Name is a system field name and the compiler can be confused if it's used as a variable. if the above were acc.name='name';` it would be clear and there'd be no ambiguity as to what you were trying to convey to the compiler.

In this case, there "might" be, particularly since you've not declared it until AFTER the above line appeared in your code. So what's above could also be the cause of your null pointer exception. The compiler may think name at the time it appears is a field name that has a value of null.

I see the same type of thing again in your test class with string name='xyz';. Again not a good practice to use. I couldn't find any reference to cite you regarding this. The Apex documentation only provides a list of reserved keywords which doesn't include 'name'. However, you'll notice in code posted around here that you don't typically see people using 'account' or 'contact' as a variable name as they're both also sObject names which can cause confusion. I think it's safe to say the same would apply to 'User', 'Name' and other common field names when it comes to use as variables.

Anyway, back to solving your problem, to further troubleshoot your issues, I recommend you move your final getter/setter statement to the top of your class plus add more debug statements; particularly following the areas I've highlighted. I'd also rename those variables, but I'll leave that decision to you.


The null reference is due to the fact you never initialize your class's instance of listcon but rather created a local version in your constructor. Remove the list<...> in your constructor and instead of a local variable you will be initializing the instance variable.


@istest public class testAccAndCons { public static testmethod void testAccAndCons1() { Account acct=new Account(name='ABC'); insert acct; system.assertNotEquals(null,acct.id);

ApexPages.StandardController sc=new ApexPages.StandardController(acct);

//covering constructors
AccAndCons ac=new AccAndCons(sc);
AccAndCons ac1=new AccAndCons(); 

//covering cancel1().
pagereference p=ac.cancel1();
system.assertEquals(' https://ap1.salesforce.com/001/o',p.geturl());

list<contact> conlist=new list<contact>();
contact con=new contact(LastName='Lname',Languages__c='abc_con');

//covering name.

//covering save().
 pagereference p1=ac.save();

} use this code hope it covers 100%.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.