+ Start a Discussion
SolidLucasSolidLucas 

test class help

Someone could help me with my test class is givin me NullPointerException

public with sharing class ContactManager {

	public Contact_cls contactAux						{get;set;}
	
	public List<Contact_cls> listContacts				{get;set;} 

	public String title									{get;set;}
	public Integer contactIdSelected					{get;set;}
	
	public boolean isEditMode							{get;set;}

	private class Contact_cls {
		public Integer id								{get;set;}			
		public Contact contato							{get;set;}
		public boolean isNew							{get;set;}
		public boolean isEdited							{get;set;}
		public boolean isDeleted						{get;set;}
		
		public Contact_cls() {
			contato = new Contact();
		}
	}

	/*
	 * CONSTRUCTOR
	 */
	public ContactManager(){
		title = 'Contato';
		isEditMode = false;
		contactAux = new Contact_cls();
	}
	
	

	
	
	/*
	 * PRIVATE METHODS
	 */
	
	/*
	 * verifica se uma lista de pagamentos(controle interno) está vazia e gera uma mensagem de erro.
	 */
	private boolean isEmpty(list<Contact_cls> pList){
		boolean result = false;
		
		if (pList.isEmpty()) {
			ApexPages.addMessage( new ApexPages.Message(ApexPages.Severity.ERROR,'É necessårio informar ao menos um contato.'));	
			result = true;
		}
		
		return result;
	}
	
	
	
	
	
	/*
	 * PUBLIC METHODS
	 */
	
	public void loadContactsByAccount(Account pAccount) {
		
		if (listContacts == null) {
			
			listContacts = new List<Contact_cls>();
		
			List<Contact> auxListContacts = [SELECT Id, Salutation, FirstName, LastName, Tipo_de_Telefone__c,
												    DDI__c, DDD__c, Phone, Ramal__c, Email
			                  				   FROM Contact
			                 				  WHERE AccountId = :pAccount.Id
		    	          				   ORDER BY LastName];
		    	          				   
			if (!auxListContacts.isEmpty()) {
				
				Integer i = 1;
				
				for (Contact iContato : auxListContacts) {
					Contact_cls cContact = new Contact_cls();
					cContact.contato = iContato.clone(true, true, true, true);
					cContact.id = i;
					cContact.isNew = false;
					cContact.isEdited = false;
					cContact.isDeleted = false;
					listContacts.add(cContact);
					i++;
				}
			}
		}
	}
	
	public PageReference newContact() {
		title = 'Contato';
		isEditMode = false;
		contactAux = new Contact_cls();
		
		return null;
	}
	
	public PageReference editContact() {
		for (Contact_cls icContato : listContacts) {
			if (icContato.id == contactIdSelected) {
				contactAux = icContato;
				title = 'Contato [Modo de Edição]';
				isEditMode = true;
				break;  
			}
		}
		
		return null;
	}

	public PageReference deleteContact() {
		for (Contact_cls icContato : listContacts) {
			if (icContato.id == contactIdSelected) {
				icContato.isDeleted = !icContato.isDeleted;
				break;  
			}
		}
		return null;
	}
	
	public PageReference saveContactInList() {
		Contact_cls cContato = new Contact_cls();
		cContato = contactAux;
		
		if (!isEditMode) {
			cContato.id = getMajorId();
			cContato.isDeleted = false;
			cContato.isEdited = false;
			cContato.isNew = true;
			listContacts.add(cContato);			
		} else {
			cContato.isDeleted = false;
			cContato.isEdited = true;
			//setContactInList(cContato);
		}
		
		newContact();
		
		return null;
	}

	private Integer getMajorId() {
		Integer retorno = 0;
		for (Contact_cls iContato : listContacts) {
			if (iContato.id > retorno) {
				retorno = iContato.id; 
			}
		}
		return retorno + 1;
	}
	

	public boolean isValid(){
		boolean result = true;
		//for (Contact_cls cContact : listContacts) {
		result = !isEmpty(listContacts);
		if (result) {
			list<Contact_cls> listWithoutDelete = new list<Contact_cls>(); 
			for (Contact_cls iContact :listContacts){
				if (!iContact.isDeleted) {
					listWithoutDelete.add(iContact);
				}
			}
. 
			if (result )
				result = !isEmpty(listWithoutDelete);
		} 
		
		return result;
	}

	public void doSave(Account pAccount) {
		
		if (pAccount != null && Functions.isNotEmptyOrNull(pAccount.Id)) {
			List<Contact> auxListInsertContact = new List<Contact>();
			List<Contact> auxListUpdateContact = new List<Contact>();
			List<Contact> auxListDeleteContact = new List<Contact>();
			
			for (Contact_cls cContact : listContacts) {
				cContact.contato.AccountId = pAccount.Id;
				
				if (!cContact.isNew && cContact.isDeleted) {
					auxListDeleteContact.add(cContact.contato);
				} else if (!cContact.isNew && cContact.isEdited) {
					auxListUpdateContact.add(cContact.contato);
				} else if (cContact.isNew) {
					auxListInsertContact.add(cContact.contato);
				}								
			}
			
			if (!auxListDeleteContact.isEmpty()) {
				delete auxListDeleteContact;
			}
			if (!auxListUpdateContact.isEmpty()) {
				update auxListUpdateContact;
			}
			if (!auxListInsertContact.isEmpty()) {
				insert auxListInsertContact;
			}
		}
	}
}

class test 20%
 
@isTest
private class ContactManager_tst {
	
    static testMethod void myUnitTest() {
    	//instanciando o controlador;
        ContactManager contact = new ContactManager();
       
        //chamando métodos do controlador
        contact.newContact();
   		contact.isValid();
        /*contact.editContact();
        
        contact.deleteContact();
        contact.saveContactInList();
        contact.doSave();**/
        
        //instanciando objeto 
         List<Contact> products = loadContacts();
         
    }
    
    // Carregando a Massa de dados para teste
    private void loadData(){
    	Account acc = new Account(
    		name = 'Teste'
    		
    	);
    	insert acc;

    
    }
    
    private static List<Contact> loadContacts(){
    	
	  	Contact cot = new Contact(
	  		Salutation = 'Sr',
    		FirstName = 'Joaquim',
    		LastName = 'Lima',
    		DDD__c = 13,
    		DDI__c = 55,
    		Tipo_de_Telefone__c = '',
    		Phone = '33778899',
    		Ramal__c = '231',
    		Email = 'joaquim.lima@gmail.com'
    	);
    	
    	Contact cot2 = new Contact();
    

	   List<Contact> contacts = new List<Contact>();
	   contacts.add(cot);
	   contacts.add(cot2);
	   insert contacts;
	   return contacts;
   	}  	
}

 
Best Answer chosen by SolidLucas
SonamSonam (Salesforce Developers) 
Check for the line where it is giving the NULL pointer exception and if that line has any variable, please have a IF condition there to check if its value is NULL and if it is - initialize it to zero.
This will help you get rid of the NULL pointer exception